[Vuejs]-Set data field from getter and add extra computed field

1👍

In situations like this you’ll want the watcher to be run as soon as the component is created. You could move the logic within a method, and then call it from both the watcher and the created hook, but there is a simpler way.

You can use the long-hand version of the watcher in order to pass the immediate: true option. That will make it run instantly as soon as the computed property is resolved.

watch: {
  allMedications: {
    handler: function (val) {
      this.medications = val.map(medication => ({
        ...medication,
        residentName: this.getResidentName(medication.resident)
      });
    },
    immediate: true
  }
}
👤Cue

Leave a comment