Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vuex选择器,如redux重新选择_Redux_Vuex_Nuxt.js - Fatal编程技术网

Vuex选择器,如redux重新选择

Vuex选择器,如redux重新选择,redux,vuex,nuxt.js,Redux,Vuex,Nuxt.js,在我的nuxt项目中,我有可以从其他商店属性推断的道具 在redux中,我会在每次有必要时,在存储发生变化时,而不一定在每次渲染时,计算这些值 如何使用nuxt/vuex实现同样的效果 谢谢我想你想要的是什么 Vuex允许我们在商店中定义“getter”。您可以将它们视为存储的计算属性。与计算属性一样,getter的结果是基于其依赖项进行缓存的,并且只有在其某些依赖项发生更改时才会重新计算 查看我链接的文档,其中有更多示例,包括如何使用基于方法的getter,它不会缓存结果 如果getter是在

在我的nuxt项目中,我有可以从其他商店属性推断的道具

在redux中,我会在每次有必要时,在存储发生变化时,而不一定在每次渲染时,计算这些值

如何使用nuxt/vuex实现同样的效果


谢谢

我想你想要的是什么

Vuex允许我们在商店中定义“getter”。您可以将它们视为存储的计算属性。与计算属性一样,getter的结果是基于其依赖项进行缓存的,并且只有在其某些依赖项发生更改时才会重新计算

查看我链接的文档,其中有更多示例,包括如何使用基于方法的getter,它不会缓存结果


如果getter是在模块中定义的,那么语法就有点不同。你需要做
store.getters['module/doneTodos']
,其中
module
是你拥有的存储模块的名称。

太棒了,这正是我想要的!
// store/index.js

export const state = () => ({
  todos: [
    { id: 1, text: '...', done: true },
    { id: 2, text: '...', done: false }
  ]
})

export const getters =  {
  doneTodos: state => {
    return state.todos.filter(todo => todo.done)
  }
}
mounted() {
  // accessing in a component
  this.$store.getters.doneTodos
}