Vue.js vue计算:从创建的生命周期挂钩添加新的计算值

Vue.js vue计算:从创建的生命周期挂钩添加新的计算值,vue.js,vue-component,Vue.js,Vue Component,我是vue的初学者 我正在尝试推送一个名为的计算数据,该名称来自创建实例之后的vuex 如何将新的计算属性推送到created()钩子中的实例 这是密码 computed: { // 3 - i want to extract the object properties here computed // that is easy bu using spread operator ... // Problem : vue i

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

如果我可以在创建后创建新的计算值,这将解决问题

我不知道您为什么要这样做,但是如果您希望在调用计算值之前有一个可用的变量,您可以使用beforeCreate挂钩:

你也可以按照

computed: {
        ...function() {
            this.stocks = this.$store.state
            let arr=[]
            for (let stock in this.stocks){
                arr.push(stock+'Getter')
            }
            return mapGetters(arr);
        }();
    },

您可以在beforeCreate钩子中执行此操作

beforeCreate: function() {
  this.$options.computed = {
     demo2: function() {
       return this.demo
     }
  }
}

请包含您正在处理的代码,并指出您已经尝试过但未成功的代码。我插入了代码。我认为这是个好主意,但这并不能解决问题,因为this.$存储区仅在创建组件后创建,并且在创建时间之前无效,因此在创建组件之前调用computed对象时,它将被删除没有看到这一点。$store.state谢谢,我认为这是个好主意,但这并不能解决问题,因为this.$store只在组件创建之后创建,在创建之前无效,所以在创建组件之前调用computed对象时,它没有看到这一点。$store.state我问的是将新的计算数据推送到computed在创建元素之后,是否有可能?我有另一个解决方案,但是在创建测试组件之前运行beforeCreate()钩子,因此它会给出错误,因为:我使用在测试组件内部创建的对象,错误的序列谢谢Gerardo Rosciano我问过在创建元素后将新的计算数据推送到计算对象,这可能吗?对不起mustafa,我不确定。