Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifier to propagate/forward events to parent #9325

Open
Vontus opened this issue Jan 15, 2019 · 18 comments · May be fixed by #10202
Open

Modifier to propagate/forward events to parent #9325

Vontus opened this issue Jan 15, 2019 · 18 comments · May be fixed by #10202

Comments

@Vontus
Copy link

Vontus commented Jan 15, 2019

What problem does this feature solve?

Currently, and as far as I know, if we want to propagate an event fired by a child component to the parent (the child's grandparent) we need to $emit the event again, and we need to pass all the arguments one more time. This can become a problem, for example, if the event has a variable number of arguments because we need to specify them manually or pass the whole array as a new argument.

The current way would be something like

@blur="$emit('blur')"
@create="$emit('create', arguments[0])"
@input="$emit('input', arguments[0], arguments[1])"

What does the proposed API look like?

@blur.propagate
@create.propagate
@input.propagate

And if we want to both handle the event and propagate it to the parent, we would use

@input.propagate="someFunction"

EDIT: Maybe since .propagate may be confused with the function .stopPropagation(), a better term could be simply .emit

@lopugit
Copy link

lopugit commented Jan 16, 2019

why not just @input="$emit('input', arguments)" ?

I do like having a dedicated keyword though

@Vontus
Copy link
Author

Vontus commented Jan 16, 2019

@lopugit because passing the whole array in one argument would make users handle the same event differently in different components, plus it is a little redundant.

@lopugit
Copy link

lopugit commented Jan 16, 2019 via email

@laander
Copy link

laander commented Jan 17, 2019

You should be able to use the spread operator, ala:

@input="$emit('input', ...arguments)"

@Vontus
Copy link
Author

Vontus commented Jan 17, 2019

@laander That solves the issue with the arguments, but I think a modifier to let Vue do it would be a nice addition.

I think this modifier can be specially useful for wrapper components that need to emit most events they receive from their child component.

@AlbertMarashi
Copy link

I think this would be a cool addition

@raisou
Copy link

raisou commented Jun 17, 2019

+1

@AlbertMarashi
Copy link

@input="$emit('input', ...arguments)" works great, however @input.propagate would be an awesome addition

Vontus pushed a commit to Vontus/vue that referenced this issue Jun 27, 2019
@Vontus Vontus linked a pull request Jun 27, 2019 that will close this issue
13 tasks
@lopugit
Copy link

lopugit commented Sep 15, 2019

nice!!! OPEN SOURCE FTW

@alecoscia
Copy link

That's also great for keeping the whole app more maintainable

@posva posva changed the title Modifier to propagate events to parent Modifier to propagate/forward events to parent Sep 26, 2019
@muodov
Copy link

muodov commented Nov 11, 2020

I believe you can achieve this by adding v-on="$listeners" on a root element of the nested component?

@Vontus
Copy link
Author

Vontus commented Nov 24, 2020

I believe you can achieve this by adding v-on="$listeners" on a root element of the nested component?

I didn't know that, thank you for the tip! The only problem I find is that you aren't able to select which events are being propagated to the parent, it just forwards every event from the nested component.

@muodov
Copy link

muodov commented Nov 24, 2020

@Vontus you can! Just wrap $listeners with your own computed property that filters the keys

@sinux-l5d
Copy link

Hey, not considered anymore?

@dospunk
Copy link

dospunk commented Oct 22, 2021

Is anyone working on this? I'd love to see this feature

@nerdcave
Copy link

I posted the same request back in 2018 for a bubble modifier, but it was rejected as syntax sugar: #7965

In Svelte you can just do <button on:click> to forward the event, so it would be really nice if you could just do
<button @click> in Vue (or at least had a modifier).

@bryanmylee
Copy link

It seems like $listeners was removed in Vue 3. What's the recommended way for building a transparent wrapping component now?

@tony19
Copy link

tony19 commented Aug 22, 2022

@bryanmylee Listeners are now in $attrs, and they begin with on (e.g., onClick).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.