我是vue的初学者
我试图用名称推送计算数据,该名称来自创建实例后出现的vuex
如何将新的计算属性推送到created()钩子中的实例?
这是代码
computed: {
// 3 - i want to extract the object properties here computed
// that is easy bu using spread operator ...
// Problem : vue intialize computed before the created() hook
// so the spreed work befor passing the filling the object
...this.mapGettersObj
},
created(){
// 1- i can access only this line after creating the object
this.stocks = this.$store.state
let arr=[]
for (let stock in this.stocks){
arr.push(stock+'Getter')
}
// 2 - mapGetters returns an object
this.mapGettersObj=mapGetters(arr)
}
如果我可以在创建后创建新的计算值来解决问题
答案 0 :(得分:2)
您可以在beforeCreate hook中执行此操作。
beforeCreate: function() {
this.$options.computed = {
demo2: function() {
return this.demo
}
}
}
答案 1 :(得分:0)
我不知道你为什么要做你做的事情,但如果你想在调用计算机之前有一个变量可用,你可以使用beforeCreate hook:https://alligator.io/vuejs/component-lifecycle/
你也可以在
行中做点什么computed: {
...function() {
this.stocks = this.$store.state
let arr=[]
for (let stock in this.stocks){
arr.push(stock+'Getter')
}
return mapGetters(arr);
}();
},