Answer the question
In order to leave comments, you need to log in
How to listen to child component events in Vue js?
Hello.
Can you please tell me how to listen to the events of the child component and change the data in the parent component depending on the value received?
The parent component has a variable isShow: false
In the child component, you need to listen for changes in the parentIsShow variable and assign it to the parent variable if there were changes.
Answer the question
In order to leave comments, you need to log in
I'll give you an example. For example, you have a parent component: Page.vue it has a button that opens the Modal.vue modal window.
Page.vue:
<button @click="isShow = !isShow">Open Modal</ button>
<Modal v-if="isShow" @closeModal="isShow = !isShow"/>
...
<button @click="$emit('closeModal')">Close Modal</button>
...
$emit('closeModal', {
data: 'value',
data2: 'value2'
})
Thanks for the comments, they gave me the right idea.
In the parent component, in the call to the child component, specified
methods: {
// Изменить переменную в родительском компоненте на значение переменной в дочернем компоненте
function() {
this.isShow = this.$refs.childComponent.parentIsShow;
}
}
methods: {
clickButtonInChildComponent() {
this.parentIsShow = terue;
this.$emit('emitChildComponent', this.parentIsShow);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question