-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
onX function removed from $attrs as soon as documented in emits prop #3432
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
Comments
Yeah that's by design. Like props, emits aren't part of I see the use case though - we don't have an event counterpart to You can make it work like this, adding it to <template>
<h2>From children:</h2>
<EmittingChildComp v-bind="{ ...$attrs, onTest1 }" />
<hr />
<h2>Inline:</h2>
<button @click="$emit('test1')">emit test1</button>
<button @click="$emit('test2')">emit test2 (will output warning)</button>
</template>
<script>
import EmittingChildComp from "./EmittingChildComp.vue";
export default {
name: "ParentComp",
inheritAttrs: false,
components: {
EmittingChildComp,
},
props: ['onTest1'],
//emits: ["test1" /*, 'test2'*/], // This is the issue: As soon as I expose that "test1" can be emitted, it is missing in $attrs!
};
</script> |
But according to the docs (https://v3.vuejs.org/guide/migration/emits-option.html#_3-x-behavior) "emits will now be included in the component's For me it is difficult to understand why documenting emits will affect what's inside I guess I can live with the |
You seem to have misread (emphasis mine):
<child-component id="myId" @click="someHandler" />
The reasoning is that
It's not common that a component would want to treat a custom event both as its own event and have it fall through to a child like you do - usually, only unhandled attributes/events are being passed down. Your case is valid of course, but not common. If we didn't remove props and events from <label>
<input v-bind="filteredAttrs">
</label> computed: {
filteredAttrs() {
return /* this.$attrs filtered to not contain stuff declared in props & emits */
}
} All that being said, we should improve documentation about this, and/or concider a better way to access the listeners of declared events than forcing devs to declare them as |
Got it. Thanks a lot for explaining! In my "real" situation I only pass it down to the child. So it is actually not a big deal for me. It think it would be really nice to have an |
Currently, if an event is declared as |
Here is a way to get the listener, which can be obtained through |
@zouyaoji Sure, but it is not for the users, we are talking about the user interface |
Closing as the original behavior being raised is by design. Whether we need a way to expose declared |
so... is there already an own thread for this? |
Version
3.0.7
Reproduction link
https://codesandbox.io/s/onx-function-removed-from-attrs-as-soon-as-documented-in-emits-prop-n9ytr?file=/src/components/ParentComp.vue
Steps to reproduce
Please see minimal reproduction. When
emits
prop is set with "test1" theonTest1
function is removed from$attrs
, so it is not possible to pass it down to a child-component, or not possible to properly use the recommended documentation what is emitted within the scope of the current component.What is expected?
shouldn't delete emit function (onTest1) from
$attrs
(especially when using inheritAttrs: false)What is actually happening?
emit is deleted for children component and not possible to pass it down.
The text was updated successfully, but these errors were encountered: