5
votes

I'm beginner in vue

I'm trying to push a computed data with name , that name come from vuex which comes after creating the instance

How can i push new computed property to the instance in the created() hook ??

Here is the code

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

If I can create new computed value after creating that will solve the problem

2
Please include the code you're working on and indicate what you've already tried that hasn't worked. - PatrickSteele
i have inserted the code - Mustafa Agamey

2 Answers

5
votes

You can do this in beforeCreate hook。

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

I don't know why you are doing what you do, but if you want to have a variable available before the computed is called you can use beforeCreate hook: https://alligator.io/vuejs/component-lifecycle/

You could also do something in the lines of

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