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.

Is it possible to emit event from component inside slot

See original GitHub issue

Vue.js version

2.1.3

Reproduction Link

https://jsfiddle.net/x8a3vvk2/8/

Events emitted from component inside slot are not received. $emit(‘testevent’) is not working but $parent.test() works.

<script src="https://unpkg.com/vue@2.1.3/dist/vue.js"></script>

<div id="app" >
  <parent>
    <child></child>
   </parent>
</div>
Vue.component('parent', {
  template: `<div><slot @testevent="test"></slot></div>`,
  methods: {
    test () {
      alert('Test ok')
    }
  }
})

Vue.component('child', {
  template: `<button @click="$emit('testevent')">I'm slot</button>`
  //template: `<button @click="$parent.test()">I'm slot</button>`
})

new Vue({
  el: '#app',
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

419reactions
yyx990803commented, Nov 29, 2016

You cannot listen to events on <slot>. It can end up rendering anything: text, plain element, multiple nodes… the behavior will be unpredictable.

It seems you are trying to make a slot container communicate with a slot child - in most cases this means the two components are coupled by-design, so you can do something like this.$parent.$emit(...) from the child, and listen to that event in the parent with this.$on(...).

I am still open to ideas on improving the ways slot parent and child can communicate, but events on <slot> doesn’t really sound like a right direction to me.

300reactions
Danonkcommented, May 27, 2018

Holy shit @TomS- , such mixing php and Vue should be punishable by death! Your code is horribly unmaintainable.

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 >
Vue emit from slot to parent - Laracasts
emit('close') but nothing is successfully sending the event to the listener. ... showModal into the slot and then listen to the close on...
Read more >
Listen on events when using slots - Get Help - Vue Forum
I don't think it is possible to listen to events on slots. However, you can listen in the child component and just emit...
Read more >
[Solved]-Emit event from content in slot to parent-Vue.js
It is not possible to listen to events emitted from the slot content by the contained component. In your case, <my-carousel> cannot listen...
Read more >
A Guide to Vue $emit - How to Emit Custom Events in Vue
Using emit , we can trigger events and pass data up the component heirarchy. This is useful for things like: emitting data from...
Read more >

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