Getter

在 scrimba 上尝试这节课

有时候我们需要从 store 中的 state 中派生出一些状态,例如对列表进行过滤并计数:

  1. computed: {
  2. doneTodosCount () {
  3. return this.$store.state.todos.filter(todo => todo.done).length
  4. }
  5. }

如果有多个组件需要用到此属性,我们要么复制这个函数,或者抽取到一个共享函数然后在多处导入它——无论哪种方式都不是很理想。

Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性)。就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

Getter 接受 state 作为其第一个参数:

  1. const store = new Vuex.Store({
  2. state: {
  3. todos: [
  4. { id: 1, text: '...', done: true },
  5. { id: 2, text: '...', done: false }
  6. ]
  7. },
  8. getters: {
  9. doneTodos: state => {
  10. return state.todos.filter(todo => todo.done)
  11. }
  12. }
  13. })

通过属性访问

Getter 会暴露为 store.getters 对象,你可以以属性的形式访问这些值:

  1. store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]

Getter 也可以接受其他 getter 作为第二个参数:

  1. getters: {
  2. // ...
  3. doneTodosCount: (state, getters) => {
  4. return getters.doneTodos.length
  5. }
  6. }
  1. store.getters.doneTodosCount // -> 1

我们可以很容易地在任何组件中使用它:

  1. computed: {
  2. doneTodosCount () {
  3. return this.$store.getters.doneTodosCount
  4. }
  5. }

注意,getter 在通过属性访问时是作为 Vue 的响应式系统的一部分缓存其中的。

通过方法访问

你也可以通过让 getter 返回一个函数,来实现给 getter 传参。在你对 store 里的数组进行查询时非常有用。

  1. getters: {
  2. // ...
  3. getTodoById: (state) => (id) => {
  4. return state.todos.find(todo => todo.id === id)
  5. }
  6. }
  1. store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }

注意,getter 在通过方法访问时,每次都会去进行调用,而不会缓存结果。

mapGetters 辅助函数

mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性:

  1. import { mapGetters } from 'vuex'
  2. export default {
  3. // ...
  4. computed: {
  5. // 使用对象展开运算符将 getter 混入 computed 对象中
  6. ...mapGetters([
  7. 'doneTodosCount',
  8. 'anotherGetter',
  9. // ...
  10. ])
  11. }
  12. }

如果你想将一个 getter 属性另取一个名字,使用对象形式:

  1. ...mapGetters({
  2. // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
  3. doneCount: 'doneTodosCount'
  4. })