2016-11-11 23 views
2

我想命名空间模块的getter,突变,行为,我看到这个document here,但它似乎有点模糊。[Vue.js] vuex中的命名空间

// 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组件?

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: ?, 
 
    }, 
 
};

+1

在 'vuex' 使用mapGetters解决的问题 – Kim

回答

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