How to emit an event from Vue.js Functional component?

Harshal Patil :

As the title of the question, this context is not available in the functional component. So if I have to emit an event, how can I do that?

For example in below code snippet:

<template functional>
    <div>
        <some-child @change="$emit('change')"></some-child>
    </div>
</template>

My functional component doesn't have this context and hence $emit is not available. How can I bubble-up this event?

Decade Moon :

This is explained in the docs Passing Attributes and Events to Child Elements/Components:

If you are using template-based functional components, you will also have to manually add attributes and listeners. Since we have access to the individual context contents, we can use data.attrs to pass along any HTML attributes and listeners (the alias for data.on) to pass along any event listeners.

At the most basic level, you can delegate all listeners like this:

<some-child v-on="listeners"></some-child>

If you only want to bind the change listener, you can do:

<some-child @change="listeners.change"></some-child>

but this will fail if listeners.change is undefined/null (not provided to the functional component).

If you need to handle the situation where there is no change listener, then you can do this abomination:

<some-child @change="listeners.change || (() => {})"></some-child>

otherwise you would have to settle by writing the render function by hand, since I don't think it is possible to conditionally assign the change listener to <some-child> in the template of a functional component. (Or maybe you can? I'm not sure.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Vue2: When testing component that contains a functional child component, how to emit event from the child?

How to make this.$emit event only affect the component the event is emitted from in Vue.JS 2

Pass or Emit an event from child component to the parent component in Vue Js with Laravel

Vue.js - Emit event from directive

Vue.js Emit event from Service

I am using Vue.js 2.0 and I am trying to emit an event from `child component`

Vue.js2 - $emit event not being captured by the parent component

Emit event from App.vue and catch in component

Vue 3 - Emit event from child component within a slot

Vue - Unable to Pass Data From Component to Parent with $emit Event

Vue.js 2 - How to $emit event from one application/instance Vue to an other?

How emit event to LoadingOverlay component from Axios custom component

How to apply classes to Vue.js Functional Component from parent component?

Vue.js Component emit

Vue js, how to get callback from emit

Emit value from vue-multiselect.js component to main view

How to emit an event and send an array to parent from modal component

Vue.js - How to emit to parent component on change in data object?

how to emit a value with a click event in vue.js

How to emit an event with Vue.js inside the bootstrap?

How to emit a Vue event into state

How to set up Vue 3 parent component to emit event to child component

How to remove event listener from Vue Component

How to bind custom event handler from vue-touch-events to a custom component in Vue.js

How to provide passed injection from vue functional component

raising an event from a child component to a container component in Vue.js?

how to render component from a component in vue js

Vue 3: Emit event from child to parent

How can I prevent multiple calls from an event listener in a Vue Js Component?

TOP Ranking

HotTag

Archive