1、import Vuex from "vuex";
2、
Vue.use(Vuex)`
3、let store = new Vuex.Store({
4、在vuex中共有四个对象分别是:
1、state (初始化状态,设置初始值)
2、mutations (修改state 中的值 【不可修改异步状态】)
mutations: {
increment (state) {
state.count++
}
}
3、active (修改state中的状态,可修改异步的)
actions: {
increment (context) {
context.commit('increment')
}
}
4、model (模块式,可分割)
const moduleA = {
state: () => ({ ... }),
mutations: { ... },
actions: { ... },
getters: { ... }
const moduleB = {
state: () => ({ ... }),
mutations: { ... },
actions: { ... }
}
const store = createStore({
modules: {
a: moduleA,
b: moduleB
}
})
store.state.a
store.state.b
}
https:
#### 最后需要在main.js中进入,并挂载
import store from './store'
new Vue({
render: h => h(App),
store,
router,
}).$mount('#app')