question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow components to listen to <slot> events

See original GitHub issue

What problem does this feature solve?

I’m aware this has been discussed previously -

Neither of which seem to lead to a solution that allows me to not tightly couple two not-necessarily related components.

I’m trying to use a datepicker component inside a datatable component as a filter, and there can be a dynamic number of datepicker components depending on what data we’re piping into the datatable. I’m also integrating this into an existing (large) codebase so I’m trying to keep all the state local to the components (no Vuex or global Vue data at all).

Ex.

<datatable api-endoint="/api/v1/orders.json">
    <datepicker filter-on="signupDate" title="Signed Up"></datepicker>
    <datepicker filter-on="lastActiveDate" title="Last Active"></datepicker>
    // etc
</datatable>

I’d love for this datepicker to be a generic date picker that can emit a “data-selected” event with the date the user selected and then wherever it’s used in the application the parent can simply listen for the event and take action (in this case re-querying the data with a date filter). Right now I have to tightly couple the datepicker to the datatable component to fire off the datatable’s filtering.

I’m just not sure I’ve seen a coherent argument for not allowing the parent to listen for the events.

What I’ve seen as a response seems to be singularly that slots won’t necessarily be a single element, but why’s that an issue?

It can end up rendering anything: text, plain element, multiple nodes… the behavior will be unpredictable.

and

As explained in #4332, it doesn’t make sense to add listeners on <slot> because <slot> doesn’t always render only a single element.

It should be no different than in Javascript with addEventListener that can be triggered from any number of child nodes.

What does the proposed API look like?

Allow a component with a <slot> to listen to events emitted from that <slot>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
posvacommented, Aug 14, 2018

Thanks for your interesting. However, nothing changes regarding existing arguments. An event is always bound to a single element/component. To me, it looks like what you need is to pass a function in a scoped slot and let the parent use that function, but your example is unclear. That could be done like this:

<datatable>
	<template slot-scope="{ doSomething }">
		<datepicker @event="doSomething" title="a"/>
		<datepicker @event="doSomething" title="b"/>
	</template>
</datatable>

Could you clarify what is exactly problematic, or what are you trying to write in your template?

1reaction
shentaocommented, Oct 2, 2018

Hey @theianjohnson, you can use Provide/Inject to provide some of the event handlers inside the <datatable> component that can be later injected into <dateFilter> component. Or any child components that chooses to inject those methods.

This might not be the solution you asked for, but at least it does not create a strong coupling like this.$parent.$emit. And with proper documentation, it’s pretty easy to manage.

Here’s an example: https://codesandbox.io/s/nny1m3j1j

As for event bubbling – events that are not caught by a parent bubble up to the root component. If for some reason you have another handler for the same event name – you’re in trouble. Makes it also much harder to see explicit connections, especially if the component tree changes so that a component is no longer an ancestor/child of a specific component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - Emit event from content in slot to parent - Stack Overflow
Slots are compiled against the parent component scope, therefore events you emit from the slot will only be received by the component the ......
Read more >
HTMLSlotElement: slotchange event - Web APIs | MDN
The slotchange event is fired on an HTMLSlotElement instance ( <slot> element) when the node(s) contained in that slot change.
Read more >
Listen on events when using slots - Get Help - Vue Forum
I want to listen /loadingFinish event emited from fetching-data-component on preloader slot . There are variations on how to do this? woodberry ...
Read more >
How to Emit Data from a Slot - Michael Thiessen
You pass a method into our slot and then call that method in the slot. You can't emit an event, because slots share...
Read more >
Vue emit from slot to parent - Laracasts
Ten Thousand Strong ... Put your @close handler onto the conditional-component tag, not the div tag since you're listening to an event of...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found