I'm using vue-property-decorator lib. I've got 2 components: parent and child, child is a component that has a prop 'modalHeader':
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
@Component({
name: "modal"
})
export default class Modal extends Vue {
@Prop({default: "Header"}) public modalHeader!: string;
}
</script>
Child component's template:
<template>
<div
class="modal fade"
id="detailsModal"
tabindex="-1"
role="dialog"
aria-labelledby="detailsModalLabel"
aria-hidden="true"
>
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="detailsModalLabel">
{{ modalHeader }}
</h5>
</div>
...
</div>
</div>
</div>
</div>
</template>
I have a property 'buttonPressed' in my parent component and I want to bind its properties to child prop, thus every time depending on the button pressed I'll get a different header for modal:
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Modal from '../modal/Modal.vue';
interface ControlButtonSetting {
id: string,
name: string,
enabled: boolean,
isDanger: boolean,
okText?: string,
componentType?: IModalComponent,
}
@Component({
name: "admin-dashboards-page",
components: {
"modal": Modal
}
})
export default class AdminDashboardsPage extends Vue {
private buttonsArray: ControlButtonSetting[] = [
{ id: "removeButton", name: "Remove", enabled: false, isDanger: true, okText: "Remove" }
];
public buttonPressed!: ControlButtonSetting;
private showModal = false;
created(): void {
this.buttonPressed = this.buttonsArray[0];
}
public controlButtonClicked(button: ControlButtonSetting): void {
this.buttonPressed = button;
this.showModal = true;
}
}
</script>
Parent component's template:
<template>
<div>
...
<!-- Modal -->
<modal
:modal-header.sync="buttonPressed.name"
<template v-slot:modalBody>
<component
:is="displayComponent()"
:dashboard-id="selectedListItemId">
</component>
</template>
</modal>
</div>
</template>
However, the 'modalHeader' value is set only once when the page loaded, but when 'selectedButton' object is changed, 'modalHeader' value is not being updated on the child component.
What helped me is to define parent's object as a Prop():
@Prop() public buttonPressed!: ControlButtonSetting;
This way child component's prop is updated whenever parent's prop is updated. But I start to see this message in a console:
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "buttonPressed"
I'm not really sure what is wrong here. Could you help me with it?
P.S. One guess I have is that I'm not mutating bound property in parent but replacing it with a new object, that's why parent-child binding may not work. Gonna check it out.