【问题标题】:Vuex, namepaced module getters not available in root getter?Vuex,命名空间的模块获取器在根获取器中不可用?
【发布时间】:2019-12-15 22:37:51
【问题描述】:

命名空间模块 getter 在 root getter、vuex 中不可用。

大家好,

我的根状态中有一个 getter,这个 getter 需要从模块(过滤器)中引用另一个 getter(getFilteredSpecialItems)。我知道rootGetters。我可以在 vue 组件中引用过滤器/getFilteredSpecialItems,所以到目前为止有效。

根状态

const getters: {
  getSortedList: (state, getters, rootState, rootGetters) {
    ...
      console.log(getters);
    console.log(rootGetters);
    console.log(getters.getFilteredSpecialItems);
    console.log(rootGetters.getFilteredSpecialItems);
    console.log(getters.filter.getFilteredSpecialItems);
    console.log(rootGetters.filter.getFilteredSpecialItems);
    ...
    }
}

过滤模块

const state: {
  namespaced: true
  ...
}

const getters: {
  getFilteredSpecialItems: (state, getters, rootState) => {
    ....
  }
}

文件夹结构

store
| index.js
| getters.js
| mutations.js
| actions.js
| state.js
| filter (dir)
| \
| | index.js
| | getters.js
| | mutations.js
| | actions.js
| | state.js

getter 和 rootGetters 都注册了过滤器模块(第一个和第二个console.log):

吸气剂

rootGetters

3.和4.console.log是undefined,5.和6.抛出错误,因为getters.filter和rootGetters.filter是undefined,所以它们没有任何属性。

如何在根 getSortedList getter 中获取 getFilteredSpecialItems?

感谢任何形式的帮助或建议。预先感谢。

【问题讨论】:

    标签: javascript vue.js vuex getter


    【解决方案1】:

    您必须像这样访问命名空间的 getter:

    rootGetters['filter/getFilteredSpecialItems']
    

    不像这样(不像状态):

    rootGetters.filter.getFilteredSpecialItems
    

    从同一个模块中的另一个 getter,你应该能够做到这一点

    getters.getFilteredSpecialItems
    

    如果它返回undefined,那么您的getter 可能返回undefined?确认它实际上正在被调用,请记住,如果自上次调用以来 getter 函数中没有可观察的数据发生变化,它的最后一个返回值可能会被缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2020-03-29
      • 2011-03-25
      • 2020-07-14
      • 2022-01-20
      • 2021-05-17
      • 2023-03-26
      相关资源
      最近更新 更多