简体   繁体   English

发出的事件不会调用 Vue JS 组件中的父方法

[英]Emitted event doesn't call parent method in Vue JS component

I have two nested Vue JS components: 'States - Parent' => 'admin-data-table - Child' .我有两个嵌套的 Vue JS 组件: 'States - Parent' => 'admin-data-table - Child'

Inside the child component there is a button that emits an event back to the parent when clicked:在子组件内部有一个按钮,单击该按钮会向父组件发送一个事件:

Child component - admin-data-table:子组件 - 管理数据表:

<template>
    <v-data-table :ref="modelName + 'Table'" :value="selectedList" @input="$emit('update:selectedList', $event)" :headers="dataTable.headers" :items="collection" :pagination.sync="dataTable.pagination" select-all item-key="id" class="elevation-1" >
        <template v-slot:items="props">
            <v-btn v-if="!props.item.active" title="Enable" color="success" small @click.prevent="$emit('toggle-active', props.item.id)"><v-icon>domain</v-icon>Enable</v-btn>
            <v-btn v-else title="Disable" color="error" small @click.prevent="$emit('toggle-active', props.item.id)"><v-icon>domain_disabled</v-icon>Disable</v-btn>
        </template>
    </v-data-table>
</template>

Inside parent component:内部父组件:

<admin-data-table @toggle-active="toggleActive"></admin-data-table>

This works fine, and the emitted 'toggle-active' event bubbles up to the parent method properly.这工作正常,并且发出'toggle-active'事件正确地冒泡到父方法。


However I want to change this to incorporate a named slot for the buttons:但是我想更改它以包含按钮的命名插槽:

admin-data-table component [child]:管理数据表组件 [子]:

<template>
    <v-data-table :ref="modelName + 'Table'" :value="selectedList" @input="$emit('update:selectedList', $event)" :headers="dataTable.headers" :items="collection" :pagination.sync="dataTable.pagination" select-all item-key="id" class="elevation-1" >
        <template v-slot:items="props">
            <slot name="rowBtns" v-bind:item="props.item"></slot>
        </template>
    </v-data-table>
</template>

Inside parent component:内部父组件:

<admin-data-table @toggle-active="toggleActive">
        <template #rowBtns="props">
            <v-btn v-if="!props.item.active" title="Enable" color="success" small @click.prevent="$emit('toggle-active', props.item.id)"><v-icon>domain</v-icon>Enable</v-btn>
            <v-btn v-else title="Disable" color="error" small @click.prevent="$emit('toggle-active', props.item.id)"><v-icon>domain_disabled</v-icon>Disable</v-btn>
        </template>
    </admin-data-table>

Now when I click on the button the toggleActive method in the parent component is no longer called when the toggle-active event is emitted.现在,当我单击按钮时,在toggle-active事件时,不再调用父组件中的toggleActive方法。 I confirmed the event is still being emitted with the correct payload when the button is pressed.我确认按下按钮时事件仍在以正确的有效负载发出。

Why is this not triggering the toggleActive parent component function anymore?为什么这不再触发toggleActive父组件功能了?

<admin-data-table @toggle-active="toggleActive">

This is explained in Compilation Scope .这在编译范围中进行了解释。

Your <v-btn> exists within the template of the parent, not the child, so you are actually calling $emit on the parent component instance and not the child.您的<v-btn>​​ 存在于父组件的模板中,而不是子组件,因此您实际上是在父组件实例而不是子组件实例上调用$emit

You do not need to use events for this situation since the parent component is managing the button itself, so you can directly call the toggleActive method from the button click event like this:在这种情况下,您不需要使用事件,因为父组件自己管理按钮,因此您可以直接从按钮单击事件中调用toggleActive方法,如下所示:

@click.prevent="toggleActive(props.item.id)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM