我正在尝试命名模块的 getter、mutations、actions,我看到这个 document here ,但似乎有点模糊。

// types.js

// define names of getters, actions and mutations as constants
// and they are prefixed by the module name `todos`
export const DONE_COUNT = 'todos/DONE_COUNT'
export const FETCH_ALL = 'todos/FETCH_ALL'
export const TOGGLE_DONE = 'todos/TOGGLE_DONE'
// modules/todos.js
import * as types from '../types'

// define getters, actions and mutations using prefixed names
const todosModule = {
  state: { todos: [] },

  getters: {
    [types.DONE_COUNT] (state) {
      // ...
    }
  },

  actions: {
    [types.FETCH_ALL] (context, payload) {
      // ...
    }
  },

  mutations: {
    [types.TOGGLE_DONE] (state, payload) {
      // ...
    }
  }
}


那么如何在 vue 组件中使用模块化的 getter、mutations?

export default {
  data() {
    // like this?
    count: this.$store.getters.DONE_COUNT,
    // ?
    count: this.$store.getters.todos.DONE_COUNT,
    // ?
    count: this.$store.getters.todosModule.DONE_COUNT,
    // ?
    count: ?,
  },
};

最佳答案

this.$store.getters['todos/DONE_COUNT']

关于javascript - [Vue.js] vuex 中的命名空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40549997/

10-15 05:37