简体   繁体   English

Vuex:在动作中引用模块变量

[英]Vuex: referencing the modules variable inside action

I have the following global store within my Vue application:我的 Vue 应用程序中有以下全局存储:

// N.B. These Stores Are Modularised, please reference: [https://vuex.vuejs.org/guide/modules.html] for details.
const store = new Vuex.Store({
  modules: {
    surfers: surfers,
    surfSites: surfSites,
    surfTickets: surfTickets
  },
  actions: {
    resetAllState: ({ dispatch, modules }) => {
      console.log(modules); // Undefined
      console.log(store.modules); // Undefined
      console.log(this.modules); // Error in v-on handler: "TypeError: _this is undefined"
      for (const currentModule in modules) {
        console.log(`Resetting Module State: ${module}`);
        if (modules[currentModule].state.hasOwnProperty("initialState")) {
          dispatch("resetModuleState", currentModule);
        }
      }
    },
    resetModuleState: (currentModule) => {
      console.log(`Resetting Module State: ${currentModule}`);
    }
  }
});

My aim is that the actions will cycle through the modules, and dispatch off a reset state action, which I call when I log the current user out.我的目标是这些动作将在模块中循环,并发送一个重置状态动作,当我注销当前用户时调用它。

However, modules is undefined, store.modules and this.modules are all undefined or through an undefined related error...但是, modules未定义, store.modulesthis.modules都未定义或通过未定义的相关错误...

So, how do I go about accessing modules dynamically in this way, if at all possible?那么,如果可能的话,我该如何以这种方式动态访问模块?

The above code is having issue, the actions can have only access to上面的代码有问题,操作只能访问

{ commit, dispatch, getters } as params

but you tried to pass modules in the params, but still you can access to the modules by below approach但是您尝试在参数中传递模块,但您仍然可以通过以下方法访问模块

use this code inside the action "resetAllState"在操作“resetAllState”中使用此代码

resetAllState: function ({ dispatch }) {
  for (const currentModule in modules) {
    this._modules.root.forEachChild((childModule) => {
      console.log(childModule);
    });
    //if (modules[currentModule].state.hasOwnProperty("initialState")) {
    //  dispatch("resetModuleState", currentModule);
    //}
  }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM