Chen
Chen

Reputation: 3060

Set new reactive properties in Vue.js

I have a parent component that pass a config object as props to its child.

I wish to add set reactive entries to this object, for example:

 created() {
   this.config.page = this.config.page || 1
 }

So from this moment, the changes for page will be reactive. Is it possible?

A not successful attempt:

beforeCreate() {
  if(!this.$options.propsData.config.page){
    Vue.set(this.$options.propsData.config, 'page', 1)
  }
}

Upvotes: 4

Views: 2554

Answers (1)

RonC
RonC

Reputation: 33917

Change it from beforeCreated to created. This should work for you I think:

 create() {
   if(!this.$options.propsData.config.page){
     Vue.set(this.$options.propsData.config, 'page', 1)
   }else{
      this.config.page = 1;
   }
 }

Upvotes: 3

Related Questions