Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue 3: emit warning even though emits is present

I receive the following warning:

[Vue warn]: Extraneous non-emits event listeners (addData) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. If the listener is intended to be a component custom event listener only, declare it using the "emits" option. 
  at <UserData onAddData=fn<bound dataSubmit> > 
  at <App>

in my Vue3 app. I use emits:["add-data"] in UserData.vue but the warning still appears. Here are the relevant parts of the vue project:

app.vue

<template>
<div class="columns">
    <div class="column">
        <user-data @add-data="dataSubmit" />
    </div>
    <div class="column">
        <active-user @delete-user="deleteUser" v-for="user in users" :key="user.id" :name="user.name" :age="user.age" :id="user.id" />
    </div>
</div>
</template>

<script>
export default {
    data() {
        return {
            users: []
        }
    },
    methods: {
        dataSubmit(name, age) {},
        deleteUser(id) {}
    }
}
</script>

UserData.vue

<template>
<h2>Add new user:</h2>
<form @submit.prevent="submitData">
    <label>Name*</label>
    <input type="text" v-model="name" placeholder="Name" />

    <label>Age*</label>
    <input type="text" v-model="age" placeholder="Age" />

    <button>add</button>
</form>
</template>

<script>
export default {
    emits: ["add-data"],
    data() {
        return {
            name: "",
            age: ""
        }
    },
    methods: {
        submitData() {
            this.$emit("add-data", this.name, this.age)
        }
    }
}
</script>

main.js

import { createApp } from 'vue'
import UserData from './components/UserData.vue'
import ActiveUser from './components/ActiveUser.vue'
import App from './App.vue'

const app = createApp(App);

app.component("active-user", ActiveUser);
app.component("user-data", UserData);

app.mount('#app')

It works fine, but it just displays the warning.

If I change the emits part to emits: ["add-data", "addData"] the warning disappears.

like image 643
miga Avatar asked Nov 21 '20 20:11

miga


1 Answers

There is an open bug for this:

https://github.com/vuejs/vue-next/issues/2540

For now you'll need to use emits: ['addData'] to avoid the warning.

like image 93
skirtle Avatar answered Sep 26 '22 12:09

skirtle