(VueJS) Vuex getter 应该根据另一个模块状态变化进行更新
(VueJS) Vuex getter should update based on another module state change
我有两个 Vuex 存储模块:ListItems
和 ListSort
。 ListItems
有一个 getter ,它基本上是根据这两个模块的状态计算出来的。请看下面的代码:
// Import the ListSort store module
import listSort from './list-sort';
// ListItems getters
const getters = {
companiesSorted: () => {
return _.orderBy(state.companies,
[listSort.state.sortAttribute],
[listSort.state.sortDirection]);
},
};
但是,当 ListSort
中的状态发生变化时(即 sortAttribute
或 sortDirection
发生变化),这不会导致 ListItems getter 重新计算。我如何告诉 Vuex,如果对计算 companiesStored
的依赖性发生了变化,Vuex 应该重新计算 getter?
谢谢!
How can I tell Vuex that, given a dependency to computing
companiesStored has changed, Vuex should recompute that getter?
您不需要,它会自动为您完成! Vue 对懒惰的开发者来说不是很棒吗?
唯一应该导入 Vuex 模块的 JS 模块是初始化 Vuex 的 JS 模块,通过 new Vuex.Store
,它将 Vuex 模块捆绑在一起。
Vuex 模块 getter 可以访问其本地 state
和 rootState
。 rootState
是您访问其他模块数据的地方。
https://vuex.vuejs.org/en/modules.html#module-local-state
它应该看起来像这样:
companiesSorted: (state, getters, rootState) => {
return _.orderBy(state.companies,
[rootState.listSortModule.sortAttribute],
[rootState.listSortModule.sortDirection]);
},
其中 listSortModule
是您为模块指定的名称。
我有两个 Vuex 存储模块:ListItems
和 ListSort
。 ListItems
有一个 getter ,它基本上是根据这两个模块的状态计算出来的。请看下面的代码:
// Import the ListSort store module
import listSort from './list-sort';
// ListItems getters
const getters = {
companiesSorted: () => {
return _.orderBy(state.companies,
[listSort.state.sortAttribute],
[listSort.state.sortDirection]);
},
};
但是,当 ListSort
中的状态发生变化时(即 sortAttribute
或 sortDirection
发生变化),这不会导致 ListItems getter 重新计算。我如何告诉 Vuex,如果对计算 companiesStored
的依赖性发生了变化,Vuex 应该重新计算 getter?
谢谢!
How can I tell Vuex that, given a dependency to computing companiesStored has changed, Vuex should recompute that getter?
您不需要,它会自动为您完成! Vue 对懒惰的开发者来说不是很棒吗?
唯一应该导入 Vuex 模块的 JS 模块是初始化 Vuex 的 JS 模块,通过 new Vuex.Store
,它将 Vuex 模块捆绑在一起。
Vuex 模块 getter 可以访问其本地 state
和 rootState
。 rootState
是您访问其他模块数据的地方。
https://vuex.vuejs.org/en/modules.html#module-local-state
它应该看起来像这样:
companiesSorted: (state, getters, rootState) => {
return _.orderBy(state.companies,
[rootState.listSortModule.sortAttribute],
[rootState.listSortModule.sortDirection]);
},
其中 listSortModule
是您为模块指定的名称。