vue v-for object props

Viewed 2459

I am trying to pass an array object to a component, but am not getting anything

create.vue

<el-row >
      <component1 v-for="product in products" :value="product" :key="product.id"></component1>
</el-row> 

//script section

data() {
    return {
       products: []  //there have (id = 1, name = first), (id = 2, name = second)
    }
}

component1.vue

<el-row>
  <div>
    {{ product.name }}
  </div>
</el-row>  

export default {
props: ['value'],
watch: {
  value: {
    hander: function (val) {
      console.log(val);

      this.product = {
        id: val.id,
        name: val.name
      } 
    },
    deep: true
  }
},
data() {
  return {
    product: {
      id: null,
      name: null
    }       
  }    
},  

but watch not worked ( {{product.name}} its null), why? or how fixed this?

1 Answers

You're trying to watch a property that doesn't change, in your example i don't figure out the need of using watch property, but you could achieve that by assigning value to your data property called product in the mounted life cycle hook as follows :

<el-row>
  <div>
    {{ product.name }}
  </div>
</el-row>  

export default {
props: ['value'],
watch: {
  value: {
    handler: function (val) {
      console.log(val);

      this.product = {
        id: val.id,
        name: val.name
      } 
    },
    deep: true
  }
},
data() {
  return {
    product: {
      id: null,
      name: null
    }       
  }    
},
mounted(){
this.product= {
        id: this.value.id,
        name: this.value.name
      } 
}
Related