在我的Laravel 9项目中,我有一个父组件,其模板包括一个子组件。
template>
....
....
<div v-if=item.is_visible class="col-4">
<note-details-modal-component v-bind:is_visible=item.is_visible :note_id= item.id>
</note-details-modal-component>
</div>在单击父组件中的一个按钮时,我将is_visible设置为true,它通过v-if呈现子组件。在子组件中,当我按下按钮时,它会调用一个向父组件发出事件的方法。
closeNow: function() {
console.log('closeNow');
// this.$parent.$emit('close_note',false);
this.$emit('close_note',false);
} , 在父组件中,我有一个方法。
close_note(value)
{
console.log('In parent');
this.is_visible = ! this.is_visible;
},当我单击子组件中的“关闭”按钮时,它在子组件中调用CloseNow(),并在console.log中看到这一点。但是,该事件不会向父事件发出。我已经尝试了我能在网上找到的所有建议。而且,我没有在Dev控制台中看到任何错误。
请有人告诉我,我的代码中有什么问题是防止事件从子组件发出到父组件的?
提前谢谢。
发布于 2022-04-07 23:26:08
问题是没有提到你所做的发射。如果你有这个:
closeNow: function() {
console.log('closeNow');
this.$emit('close_note',false);
}在调用子组件时,应该提到close_note。应该是这样的:
<note-details-modal-component v-bind:is_visible=item.is_visible :note_id= item.id @theEmitName="theFunctionYoucall">
</note-details-modal-component>其中theEmitName是close_note,您调用的函数具有相同的名称。这个介质是有用的:https://medium.com/js-dojo/component-communication-in-vue-js-ca8b591d7efa
https://stackoverflow.com/questions/71789767
复制相似问题