Is it possible to emit event from component inside slot
See original GitHub issueVue.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:
- Created 7 years ago
- Reactions:19
- Comments:24 (2 by maintainers)
Top 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 >Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 withthis.$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.Holy shit @TomS- , such mixing php and Vue should be punishable by death! Your code is horribly unmaintainable.