0

So I have a parent component Store that has a child component called Order. The data from the parent is passed via props to its child, like so:

parent

<Order :order="order" />

child

props: ['order']

I need to use a new data variable in the child component called orderIds, so I declare that:

child

props: ['order'],
data: () {
  return {
    orderIds: Object.keys(this.order)
  }
}

Then, I render the orderIds in the view:

{{orderIds}}

Now, whenever the order data changes in the parent, it updates the prop order in the child, but it is not propagated through to orderIds.

How would I update orderIds when something changes in order?

0

1 Answer 1

4

Instead of data use a computed property.

computed: {
    orderIds: function () {
      return Object.keys(this.order);
    }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

But this way, we can't update orderIds in child component. It is common that we use props to initialize data and change data afterwards.
You can emit an event to update the order on the parent component and hence updating the orderId on child component, or you could use a computed setter, or you could initialize data with order and use computed property based on that. The answer I added was appropriate for the asked question.

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.