【发布时间】:2018-08-31 01:17:31
【问题描述】:
有可能吗?
我知道我可以这样做:
computed: {
...mapGetters({
xyz: 'xyz'
}),
但我想知道是否可以使用继承的 mapGetters 来做到这一点,即在 created 钩子中:
created () {
this.$options.computed = Object.assign(this.$options.computed, {...this.dependencies.mapGetters(this.stores)});
console.log(this.$options.computed); // returns mapped getters
},
控制台输出返回映射的 getter,但它似乎不起作用,在 Vue DevTools 中那些 Vuex 绑定是未定义的。
【问题讨论】:
-
你为什么要这样做?
-
我有组件 xyz 和 A、B、C 组件正在扩展 xyz。我宁愿在 xyz 中导入
mapGetters,而不是在每个 A、B、C 中。 -
“扩展”使用
Vue.extend()? -
@acdcjunior 是的
标签: javascript vue.js vuejs2 vuex