Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

vuejs 3: declare an emit event in a child component via emits option

Using Vue 3.2 and I’m under the impression that I follow the documentation

I’m sending an array of objects to a child component and in return, I am trying to emit an event to the parent compenent so one object (identified by id) gets deleted from the array.

// parent
<template>
    <TheAccountLines :lines=recurrents_credits @goaway(id)="goaway(id)" />
</template>

<script setup>
import { find } from "lodash"
// this doesn't get called at all, just putting it here to show that the function exists.
const goaway = (id) => {
  var line = find (state.accounts, {id:id})
  console.log(line)
}
</script>
// child component
<template>
 <div v-for="line in lines" :key="line.id">
    <span @click="goaway(line.id)" >X</span>
    {{ line.foo) }}
    {{ line.bar}}
  </div>
</template>
<script setup>
const props = defineProps({ 'lines': Array)}
const emit = defineEmits(['goaway'])
const goaway = (id) => emit('goaway', id)

</script>

Upon page load, I get this message in the console: Extraneous non-emits event listeners (goaway(id)) 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I understand the "emits" option to be defineProps. But I must be wrong.

>Solution :

I think the problem is: you should use @goaway, but not @goaway(id).

Part of template:

<template>
<!--- THIS IS WRONG, SEE UPDATE BELOW -->
  <TheAccountLines :lines="recurrents_credits" @goaway(id)="goaway(id)" />
</template>

BTW, you can use onGoaway prop instand @goaway:

// parent component
<template>
  <TheAccountLines :lines="recurrents_credits" @goaway="goaway" />
</template>

// child component
<script setup>
const props = defineProps(['onGoaway'])

const emitDelete = (id) => { props.goaway(id) }
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading