【问题标题】:Ho do you call Vuex module Action from Vue component action?您如何从 Vue 组件操作中调用 Vuex 模块操作?
【发布时间】:2019-08-16 13:21:03
【问题描述】:

假设我有 Vuex 存储模块storeModuleName,我想从某个组件调用它的操作。

组件:


export default {
  actions: {
   close() {
     dispatch('storeModuleName/storeModuleAction');
   },
  }
...

错误:

dispatch is not defined

【问题讨论】:

    标签: vuejs2 vuex


    【解决方案1】:

    你需要mapActions,mapActions 让你与模块内的动作连接

    import { mapActions } from "vuex";
    export default {
      methods: {
        ...mapActions({
          storeModuleActionName: "storeModuleName/storeModuleAction"
        })
      },
      mounted(){
        this.storeModuleActionName();
      }
    }
    

    然后你可以使用storeModuleActionName作为正常方法,Check the link about mapActions

    【讨论】:

      猜你喜欢
      • 2017-11-20
      • 2019-12-18
      • 2021-07-27
      • 2018-04-25
      • 2022-01-23
      • 2019-10-18
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多