-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
emits property blocks $attrs injection #4736
Comments
There's a workaround by defining the event in <template>
<div>
<Child v-bind="{ ...$attrs, onMyEvent }" />
<button @click="$emit('my-event')">Mid emit</button>
</div>
</template>
<script>
import Child from "./Child.vue";
export default {
//emits: ["myEvent"],
props: {
onMyEvent: Function,
},
inheritAttrs: false,
components: { Child },
};
</script> The cleaner solution would likely be to provide a way of accessing the events that were declared with |
Thanks @LinusBorg that's a nice workaround! Hopefully, we can get a proper solution 🙏🏻 |
To document another workaround, explicitly listening for and then re-emitting the event also works. <template>
<div>
<Child v-bind="$attrs" @myEvent="$emit('myEvent')" />
<button @click="$emit('my-event')">Mid emit</button>
</div>
</template>
<script>
import Child from "./Child.vue";
export default {
emits: ["myEvent"],
inheritAttrs: false,
components: { Child },
};
</script> |
This behavior is expected (vuejs/rfcs#154 (comment)) so it should maybe be documented instead. This is a duplicate of #3432, #4736, #4489. Given #3432 (comment), this should go into a discussion in the rfcs repo with a proposal to enable You can find a well-described example of how to handle this in this reply by @LinusBorg @marina-mosti can you open a new discussion in the rfcs repo? |
As suggested, discussion open here: vuejs/rfcs#397 |
Version
3.2.19
Reproduction link
codesandbox.io
Steps to reproduce
Having a component that both declares
emits
because it itself emits an event (seeMid
component in sandbox) and that also expects to pass downonMyEvent
down as $attr fallthrough to aChild
does not work.The
emits
option effectively makes theonMyEvent
attribute disappear from $attrs, and the chain is broken.What is expected?
emits
property should not removeonX
from $attrsWhat is actually happening?
It gets removed and breaks attribute event inheritance
The text was updated successfully, but these errors were encountered: