H5W3
当前位置:H5W3 > 其他技术问题 > 正文

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
})

回答:

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

import * as actions from './actions'
import * as getters from './getters'

回答:

...mapGetters([
      'theCount'
    ])

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

回答:

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

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

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

本文地址:H5W3 » vuex2中mapGetters映射不到对应的getter,提示[vuex] unknown getter?

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址