vue 组件如何调用 vuex 模块中的getters

  1. 我在car模块中自己的定义了state, getters,

  2. this.$store.state.car.list可以拿到值.

  3. 但是,this.$store.state.car.carGetter报错,

  4. 请问.如何在组件中调用这个getters,

//car.js
state = {
    list: []
}
getters = {
    carGetter: state => {
        return state.list.filter('');
    }
}
new Vuex.Store({
    getters: {
        test: state => {
            return '02';
        } 
    },
    modules: { car }    
})
// 组件
this.$store.state.car.list // OK
this.$store.state.car.carGetter  // undefined
this.$store.state.carGetter  // 为什么这么用ok, 难道会把模块中的getters注册到root ?

已解决

模块内部的 action、mutation、和 getter 现在仍然注册在全局命名空间——这样保证了多个模块能够响应同一 mutation 或 action。

阅读 13.6k
3 个回答

组件中建议使用mapGetters

computed:{
    ...mapGetters('carGetter')
},
methods:{
    someFunction(){
        console.log(this.carGetter);
    }
}

你在car 模块中的getters 其实已经注册在全局getters 里面了

我的解决方法

...mapGetters(
    {carGetter(可以别名,防止冲突):'carGetter'}
)

楼主,同遇到这种问题,解决了吗?理解不过来有点

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