store.js文件vue
import Vue from 'vue'vuex
import Vuex from 'vuex'函數
Vue.use(Vuex)this
export default Vuex.Store({spa
state: { // this.$store.state.num事件
num: 1get
},it
getters: { // this.$store.getters.countio
count(state){console
return state.num + 1
}
},
mutations: {
ADD(state){ // this.$store.commit("ADD")
state.num ++;
}
},
actions: {
ADD({commit}){ // this.$store.dispatch("ADD")
commit("ADD")
}
ADD(context){
context.commit("ADD")
}
}
})
輔助函數 mapGetters,mapActions
組件:
<p>{{count}}</p>
<p @click="ADD">點擊事件</p>
import {mapGetters, mapActions} from 'vuex'
computed: {
...mapGetters["count"],
title( ){
return this.title + 'aa'
}
},
methods: {
...mapActions["ADD"],
add( ){
console.log("本文件方法")
}
}