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

查看:1499
本文介绍了vue.js - 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。

解决方案

组件中建议使用mapGetters

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

这篇关于vue.js - vue 组件如何调用 vuex 模块中的getters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆