相关疑难解决方法(0)

VueJs + Vuex + mapActions

在文档中,除了通过动作调用的突变之外,状态是不可变的......好吧。

我在我的组件中使用 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:我在我的商店中使用模块

谢谢

vuex vuejs2

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

vuejs2 ×1

vuex ×1