一、安装vuex依赖
npm install --save vuex
二、main.js引入
import Vuex from 'vuex';
Vue.use(Vuex);
三、创建store文件夹
gxtStore.js
export default {
namespace: 'gxtStore',
state: {
idx: 1,
star: 'yangmi'
},
mutation: {
//同步
},
action: {
//异步
}
}
index.js引入gxtStore.js
import gxtStore from './gxtStore.js';
export default {
modules: { //对外暴露gxtStore
gxtStore: gxtStore
}
}
main.js引入index.js
import store from './store/index.js';
new Vue({
el:'#app',
render(h){
return h(app)
},
store: new Vuex.Store(store) new一哈
})
在Vue中使用
{{$store.state.gxtStore.idx}}
{{$store.state.gxtStore.star}}//gxtStore是namespace