vuex2中mapGetters映射不到对应的getter,提示[vuex] unknown getter?

之前vuex1的时候是可以的,但是升级到vuex2之后,就提示找不到getter

<template>
  <div>
    <h3>Count is {{ theCount }}</h3>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  computed: {
    ...mapGetters([
      'theCount'
    ])
  }
}
</script>
// getters.js
export const theCount = state => state.count
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import getters from './getters'
import * as Types from './mutation-types'

Vue.use(Vuex)

const state = {
    count: 0
}

const mutations = {
    [Types.INCREMENT] (state, amount) {
        state.count = state.count + amount;
        return true;
    },
    [Types.DECREMENT] (state, amount) {
        if (state.count < amount) return false;
        state.count = state.count - amount;
        return true;
    }
}

export default new Vuex.Store({
    actions,
    getters,
    state,
    mutations
})
阅读 19.9k
4 个回答

额。。自己找到问题了。。import actions 和 getters 的时候,没写全。。应该这样

import * as actions from './actions'
import * as getters from './getters'
新手上路,请多包涵
...mapGetters([
      'theCount'
    ])

写上这三个点报错,你是怎么配置的呀?

新手上路,请多包涵

虽然是很老的问题,但是看浏览次数很多,还是上来说下。

...mapGetters('cart', {
      products: 'cartProducts',
      total: 'cartTotalPrice'
    })

这是 vuex 购物车样例的代码,报错的原因是缺少了 namespace ,也就是 'cart'。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题