import Vuex from ‘vuex’
Vue.use(Vuex)
//创建store仓库
const store =new Vuex.Store({
state:{
count:0
},
mutations:{
increment (state){
state.count++;
},
derease(state){
state.count--;
},
actions:{
increment(context){
setTimeout(function(){
context.commit(‘increment’);
},1000
)
},
decrease(context){
setTimeout(function(){
context.commit(‘decrease’);
},1000
)
}
},
getters:{
getState(state){
return state.count>0?state.count : 0
}
}
}
})
new vue({
el:’#app’,
template:’<app/>’,
store,
components:{
App
}
})
//读取state this.$store.state.count;
//读取函数mutations: this.$store.commit(‘increment’)
//异步的读取函数actions this.$store.despatch(‘decrease’) this.$tore.despatch(‘increment’)
// getters相当于计算属性 this.$store.getters.getState