在文档中,除了通过动作调用的突变之外,状态是不可变的......好吧。
我在我的组件中使用 mapGetters、mapActions ...
店铺 :
export default {
namespaced: true,
state: {
color: "violet"
},
mutations: {
changeColor(state, newColor) {
state.color = newColor
},
},
actions: {
changeColor({ commit }, newColor) {
commit('changeColor', newColor)
}
}
Run Code Online (Sandbox Code Playgroud)
成分 :
...
methods: {
...mapActions({
setColor: 'store/changeColor',
}),
myMethodCallByButton(){
this.setColor("blue").then(response => {
console.log("change Color done")
},err => {
console.log("change Color error")
})
}
...
Run Code Online (Sandbox Code Playgroud)
该方法工作正常,商店已更新,除了我从未收到过 console.log ()。
文档中写道,mapActions 相当于 this.$store.dispatch。
PS:我想保留 mapGetters 地图,mapActions .. 我不喜欢调用 this.$store.dispatch
PS2:我在我的商店中使用模块
谢谢