最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js - components data do not update - Stack Overflow

programmeradmin0浏览0评论

I have a Vue list that is based of an array and each array item renders a ponent where I bind the array item properties.

  <div v-for="item in items">
      <item v-bind:item="item"></item>
  </div>

This ponent has a mixed data, based on the binded properties

Vueponent('item', {
  template: '<p>ID: {{item.id}}, {{ponent_id}}</p>',
  props: ['item'],
  data: function() {
    return {
      ponent_id: this.item.id
    }
  }
});

The problem is that when I change the initial list array in any way, the mixed prop of the ponent maintains it's original update and does not change, even if the original binded data changes.

How can I make the ponent to update it's ow data property?

I have a Vue list that is based of an array and each array item renders a ponent where I bind the array item properties.

  <div v-for="item in items">
      <item v-bind:item="item"></item>
  </div>

This ponent has a mixed data, based on the binded properties

Vue.ponent('item', {
  template: '<p>ID: {{item.id}}, {{ponent_id}}</p>',
  props: ['item'],
  data: function() {
    return {
      ponent_id: this.item.id
    }
  }
});

The problem is that when I change the initial list array in any way, the mixed prop of the ponent maintains it's original update and does not change, even if the original binded data changes.

http://codepen.io/anything/pen/bgQBwQ

How can I make the ponent to update it's ow data property?

Share Improve this question asked Feb 10, 2017 at 13:05 Adrian FlorescuAdrian Florescu 4,50210 gold badges52 silver badges77 bronze badges 3
  • 3 Shouldn't you be using a puted property in this case? – UnholySheep Commented Feb 10, 2017 at 13:09
  • @UnholySheep YES! Thanks. I am really new to vuejs and I was not aware of the puted property, it works now. Thanks a ton! codepen.io/anything/pen/GrwNew – Adrian Florescu Commented Feb 10, 2017 at 13:14
  • @UnholySheep, could you add your answer in order to vote it and help other people? – Mihai Alexandru-Ionut Commented Feb 10, 2017 at 13:22
Add a ment  | 

1 Answer 1

Reset to default 3

As requested in the form of an answer:

In this case a puted property is the correct approach, leading to the following code:

Vue.ponent('item', {
  template: '<p>Original: {{item.id}}, Mixed: {{ponent_id}}, Computed: {{puted_id}}</p>',
  props: ['item'],
  puted: {
    puted_id: function() {
      return this.item.id;
    }
  }
});

This way the puted_id will be (correctly) reputed every time the item prop changes.

发布评论

评论列表(0)

  1. 暂无评论