6

Hi everyone I just want some explanation about vue props data. So I'm passing value from parent component to child component. The thing is when parent data has data changes/update it's not updating in child component.

Vue.component('child-component', {
  template: '<div class="child">{{val}}</div>',
  props: ['testData'],
  data: function () {
    return {
        val: this.testData
    }
  }
});

But using the props name {{testdata}} it's displaying the data from parent properly

Vue.component('child-component', {
  template: '<div class="child">{{testData}}</div>',
  props: ['testData'],
  data: function () {
    return {
        val: this.testData
    }
  }
});

Thanks in advance

Fiddle link

7
  • 1
    You're creating a copy in val: this.testData. This is pretty much down to JS references. Commented Mar 22, 2018 at 2:01
  • See vuejs.org/v2/guide/components.html#One-Way-Data-Flow Commented Mar 22, 2018 at 2:03
  • thanks @Phil, but does val wont update when values changes Commented Mar 22, 2018 at 2:03
  • Also, what is your question? Commented Mar 22, 2018 at 2:03
  • 3
    The data function is called once in the component lifetime, which means that val is set when the component is created. There is nothing that automatically updates val in your code. You likely want val to be a computed property, or just render the prop. Or you would watch testData and update val when a change occurs. Commented Mar 22, 2018 at 2:07

2 Answers 2

7

This is best explained with a very simple example

let a = 'foo'
let b = a
a = 'bar'

console.info('a', a)
console.info('b', b)

When you assign...

val: this.testData

you're setting the initial value of val once when the component is created. Changes to the prop will not be reflected in val in the same way that changes to a above are not reflected in b.

See https://v2.vuejs.org/v2/guide/components.html#One-Way-Data-Flow

Sign up to request clarification or add additional context in comments.

6 Comments

this doesn't answer the question of how to make the props dynamic.
@grantzukowski that's not what OP was asking. This answers the question asked. In fact, I have no idea what you're talking about
He was asking how to make the passed in prop value dynamic, so if it changes, the child reacts.
@grantzukowski no "he" is not. I suggest you read the question and the comments on the question again. OP's question makes it quite clear that they're able to see changes to the passed in prop values (see second snippet)
Its literally exactly what I said. I suggest you read the question again. In this case, I think the prop needs to be passed in as a computed value and there needs to be a watcher in the child. Explaining the concept is not answering the question. stackoverflow.com/questions/50035675/…
|
0

I resolve with! this.$set(this.mesas, i, event);

data() {
    return { mesas: [] }
},
components: {
    'table-item': table_l,    
},
methods: {
    deleteMesa: function(event) {
        let i = this.mesas.map(item => item.id).indexOf(event.id);
        console.log("mesa a borrare", i); 
        this.mesas.splice(i, 1);
    },
    updateMesa: function(event) {
        let i =this.mesas.map(item => item.id).indexOf(event.id);
        console.log("mesa a actualizar", i); 
        ///    With this method Vue say Warn
        //this.mesas[i]=event;  
        /// i Resolve as follow
        this.$set(this.mesas, i, event);
     },
     // Adds Useres online
     addMesa: function(me) {
         console.log(me);
         this.mesas.push(me);
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.