在Vue父母与子女之间传递事件

时间:2019-01-05 08:58:03

标签: vue.js

我试图从孩子那里发出一个事件并在父母那里听,但是我总是遇到类似Invalid handler for event "new-tab": got undefined的错误。

我的应用程序:

<div class="ibox" id="app">
    <div class="ibox-content">
        <h2>Table</h2>
        <div class="details">
            <table-tabs
                    v-on:new-tab="newTab"
                    v-on:close-tab="closeTab"
                    ref="tableTabs"
                    name="table-tabs"
            ></table-tabs>
        </div>
    </div>
</div>

TableTabs组件:

<template>
    <div>
        <b-card no-body>
            <b-tabs card>
                <b-tab active>
                    <template slot="title">
                        <i class="fa fa-tablet-alt"></i> Orders
                    </template>
                    <orders-table
                            name="orders-table"
                    ></orders-table>
                </b-tab>
                <b-tab v-for="order in tabs" :key="i">
                    <template slot="title">
                        <div>{{ order.name }}</div>
                        <b-button type="button" class="close float-right" aria-label="Close" @click="closeTab(order.id)">
                            <span aria-hidden="true">&times;</span>
                        </b-button>
                    </template>
                    <items-table
                            name="items-table"
                            :api-url="'/api/items/' + order.id"
                    ></items-table>
                </b-tab>
            </b-tabs>
        </b-card>
    </div>
</template>

<script>
    export default {
        name: 'table-tabs',
        data() {
            return {
                tabs: [],
            }
        },
        methods: {
            closeTab(id) {
                for (let i = 0; i < this.tabs.length; i++) {
                    if (this.tabs[i].id === id) {
                        this.tabs.splice(i, 1);
                    }
                }
            },
            newTab(item) {
                this.tabs.push(item);
            }
        }
    }
</script>

OrdersTable组件:

<template>
    <div>
        <vuetable ref="vuetable"
                  :api-url="apiUrl"
                  :fields="fields"
                  pagination-path="pagination"
                  @vuetable:pagination-data="onPaginationData"
        >
            <div slot="name-slot" slot-scope="{ rowData }">
                <a href="#" class="float-left" @click="newTab(rowData.order)">
                    {{ rowData.order.name }}
                    <span class="fa fa-search-plus"></span>
                </a>
            </div>
        </vuetable>
        <div class="row">
            <div class="col-md-6">
                <vuetable-pagination-info
                        ref="paginationInfo"
                ></vuetable-pagination-info>
            </div>
            <div class="col-md-6">
                <vuetable-pagination-bootstrap
                        ref="pagination"
                        class="pull-right"
                        @vuetable-pagination:change-page="onChangePage"
                ></vuetable-pagination-bootstrap>
            </div>
        </div>
    </div>
</template>

<script>
    import Vuetable from 'vuetable-2/src/components/Vuetable';
    import VuetablePaginationBootstrap from '../VuetablePaginationBootstrap';
    import VuetablePaginationInfo from 'vuetable-2/src/components/VuetablePaginationInfo';
    import TableFieldDef from './table-field-def';

    export default {
        name: 'orders-table',
        components: {
            Vuetable,
            VuetablePaginationBootstrap,
            VuetablePaginationInfo
        },
        data() {
            return {
                apiUrl: '/api/orders?include=items',
                fields: TableFieldDef,
            }
        },
        methods: {
            newTab(order) {
                this.$emit('new-tab', order);
            }
        }
    }
</script>

为什么this.$emit('new-tab', order)总是导致未定义处理程序newTab

Vue 2.5.21

1 个答案:

答案 0 :(得分:0)

在这种情况下,父级是TableTabs组件。如果父级需要侦听子级组件发出的事件,则需要将事件侦听器添加到子级组件,在本例中为OrdersTable组件。所以代替这个..

<table-tabs
  v-on:new-tab="newTab"
  v-on:close-tab="closeTab"
  ref="tableTabs"
  name="table-tabs"
></table-tabs>

您应该执行此操作(在TableTabs组件内部)。

<orders-table
  name="orders-table"
  v-on:new-tab="newTab"
></orders-table>