0

I have a problem with a vue component that handles model changes.

In the parent component I have something like this:

<div v-for="role in roles">
    Start date: {{ role.start_date) }}
    <button @click="handleSelectRoleToEdit($event, role)" id="start_date"</button>
</div>

<edit-role
    :role="selectedRole"
    :editField="editField"
    ref="ToEditRole"
    >
</edit-role>

where handleSelectRoleToEdit is

handleSelectRoleToEdit(event, role) {
    this.selectedRole = role;
    this.$refs.ToEditRole.editRoleModal()
    this.editField = event.currentTarget.id
}

This piece of code should take the current item and pass it to the child component <edit-role>. Inside the child component there is

props: [
    "role", 
    "editField"
]

...

editRoleModal() {
    this.modal_edit_role = new Modal(document.getElementById('edit_roleModal'), {})
    this.modal_edit_role.show()
    console.log(this.role)
}

The modal is successfully created and opened, but console.log(this.role) returns Proxy {}.
If I edit the fields (so I call another function), everything is fine, because this.role becomes available.

However I need this.role to be available as soon as the modal is created.

How can I solve this problem?

Any help is greatly appreciated

Leonardo
  • 2,439
  • 33
  • 17
  • 31
  • Please review carefully, specially "suggested edit" queue. Look at the post in a whole: stackoverflow.com/review/suggested-edits/30274348, stackoverflow.com/review/suggested-edits/30261069, https://… and many more! For example, the first wasn't an answer at all, so the edit is not an improvement – Vega Nov 07 '21 at 08:58

1 Answers1

0

I don't know if this suits your needs, but instead of using $refs you could set a watcher for the prop role in the child component. This ensures that the function is executed when the data is available.

Mikel Granero
  • 379
  • 6
  • 15