- 在src下新建 store文件夹,里面有index.js和modules
- 在main.js里引入store
import store from './store'
new Vue({
el: '#app',
store,
render: h => h(App)
})
import Vue from 'vue'
import Vuex from 'vuex'
//这里是引入modules里的子仓库
import selectStatusStore from './modules/selectStatusStore'
import exerciseBtnStore from './modules/exerciseBtnStore'
Vue.use(Vuex)
const state = () => ({
})
const mutations = {
}
const actions = {
}
const getters = {
}
const store = new Vuex.Store({
state, // 挂载存取数据功能
getters, //挂载数据计算功能
mutations, // 挂载函数功能
actions, // 挂载异步函数
modules:{ // 挂载子仓库
selectStatusStore,
exerciseBtnStore
}
});
export default store
const state = () => ({
})
const mutations = {
}
const actions = {
}
const getters = {
}
export default {
namespaced: true, //这个可以让在组件中直接通过mapState引用子仓库
state,
getters,
actions,
mutations
};
import { mapState } from "vuex";
export default {
computed: {
...mapState("selectStatusStore", ["state里的属性"]),
},
}