vuex基本使用

vuex使用

安装

npm install vue

引入

src目录下创建store/index.js

//index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  }
})

入口文件main.js引入store

//main.js
import Vue from 'vue'
import store from './store'

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

五大核心

state:全局状态数据

getters:相当与computed

mutations:规定只有mutation才能修改state,通过commit方法触发

actions:异步操作,通过dispatch触发mutation

posted @ 2020-03-02 00:45  zhaoyifeng  阅读(88)  评论(0)    收藏  举报