1

I have two Vue components, EventTask and EventCard. EventTask has a currentEvent object in data, which I pass as a prop to EventCard like so

<event-card :current-event="currentEvent" />

In EventCard, I initialise an event data property from the currentEvent prop, as suggested in this answer

export default {
  name: 'EventCard',
  props: {
    currentEvent: {
      type: Object,
      required: false
    }
  },
  data: function () {
    return {
      event: { ...this.currentEvent }
    }
  }
}

However, for some reason, the event data property is not being set correctly. Here's what I see in the Vue developer tools

enter image description here

Notice that the currentEvent prop is an object with a with a bunch of properties, but the event data property is an empty object. Why is the data property not being initialised correctly from the prop?

3
  • 1
    Can you share a simple demo/fiddle representing your scenario, because it works in normal cases. Commented Jul 30, 2020 at 19:07
  • why do you need to create another property in data? you can use the same currentEvent (this.currentEvent) in your EventCard component. Any specific reason for creating another property in data?. The other way is to assign the currentEvent to event (in data) in one of the life cycles, you can use mounted() {this.event = this.currentEvent } Commented Jul 30, 2020 at 19:11
  • @SowmyadharGourishetty I need to create another property in data because I will be modifying this object in the EventCard component, but I don't want these changes to propogate to the EventTask component Commented Jul 30, 2020 at 19:21

3 Answers 3

2

This can occur if currentEvent is updated after EventCard has already initialized. Note that data() is not invoked reactively, so changes to currentEvent would not re-initialize event.

One solution is to use a watcher on currentEvent to copy it to event:

export default {
  watch: {
    currentEvent(newValue) {
      this.event = { ...newValue }
    }
  }
}

demo

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

1 Comment

I think currentEvent being updated after EventCard has initialized is exactly what the problem is. I didn't realise that data() is invoked only once, rather than reactively.
0

you can try the below code

export default {
  name: 'EventCard',
  props: {
    currentEvent: {
      type: Object,
      required: false
    }
  },
  data: function () {
    return {
      event: null
    }
  },
  mounted () {
    this.event = this.currentEvent // you can also try clone here.
  }
}

Comments

0

Try doing it like this instead:

export default {
  name: 'EventCard',
  props: {
    currentEvent: {
      type: Object,
      required: false
    }
  },
  data: function () {
    return {
      event: this.currentEvent
    }
  }
}

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

2 Comments

AFAIK, it's recommended that you don't do this when the prop is an object or array (which are passed by reference), because changes in the child will also be reflected in the parent. Source stackoverflow.com/a/40435856/2648
Okay! Thanks for the info

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.