不工作... mapGetters

时间:2018-05-08 12:41:06

标签: vue.js vuex

  

错误[vuex] unknown getter:counter / count`

结构:

store.js

const state = {
    someRootState: 'someRootState'
}

const getters = {
    getRootState: state => state.someRootState
}

import counter from './counter.js'

export default new Vuex.Store({
    state,
    getters,
    modules:{
        counter
    }
})

counter.js

const state = {
    count: 10
}

const getters = {
    getCount: state => state.count
}

export default {
    namespaced: true,
    state,
    getters
}

App.vue

import { mapGetters } from "vuex";

computed: {
    //doesn't work  [vuex] unknown getter: counter/count
    ...mapGetters("counter", [
        "count"
    ]),

    //doesn't work  [vuex] unknown getter: counter/count
    ...mapGetters({
        count: "counter/count"
    }),

    //work
    ...mapGetters(["getRootState"]),

    //work
    ...mapGetters({
        getRootState: "getRootState"
    }),

}

mapStates工作正确,mapGetters不工作,请帮助我理解

1 个答案:

答案 0 :(得分:3)

您的getter名为getCount,而非count,因此mapGetters调用应如下所示:

...mapGetters("counter", [
  "getCount"
]),