【问题标题】:[Vue.js]Namespacing in vuex[Vue.js]vuex 中的命名空间
【发布时间】:2016-11-11 14:24:03
【问题描述】:

我正在尝试命名空间模块的 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: ?,
  },
};

【问题讨论】:

  • 通过在'vuex'中使用mapGetters解决了这个问题

标签: javascript vue.js vuex


【解决方案1】:
this.$store.getters['todos/DONE_COUNT']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 2020-05-07
    • 2019-07-02
    • 1970-01-01
    • 2020-03-29
    • 2023-03-19
    • 2019-09-19
    • 2018-05-27
    相关资源
    最近更新 更多