Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue computed : add new computed value from created lifecycle hook

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

like image 249
Mustafa Agamey Avatar asked May 04 '17 11:05

Mustafa Agamey


Video Answer


1 Answers

You can do this in beforeCreate hook。

beforeCreate: function() {
  this.$options.computed = {
     demo2: function() {
       return this.demo
     }
  }
}
like image 114
undefined Avatar answered Oct 06 '22 20:10

undefined