Why does this work? (event-listner prop) #14107
-
|
Hi I am confused over here, I made a modal component that I can feed with function props. These can even be validated using runtime validation (as stated here). It feels like an anti-pattern as soon as you use a prop name called "onXxx" in this example, I used
<template>
<div>
<button @click.self="runCallstack">Callback button</button>
<p v-if="props.onCallback">Callback function detected</p>
<p>
{{ callbackCalled }}
</p>
</div>
</template>
<script setup lang="ts">
import { ref } from "process";
import { withDefaults } from "vue";
interface ComponentProps {
onCallback?: () => Promise<void>;
}
const props = withDefaults(defineProps<ComponentProps>(), {
onCallback: undefined,
});
const callbackCalled = ref(false);
const emit = defineEmits(["callbackCalled", "callbackFailed"]);
async function runCallstack() {
try {
// Do some pre work
if (props.onCallback) await props.onCallback();
// Do some post work
} catch {
// maybe do a catch or something and emit that
emit("callbackFailed");
} finally {
// when done with it all emit that
emit("callbackCalled");
}
}
</script>The fun actually starts when you use this component.
<callback-component @callback="async () => console.log('hello world')" />As you can see, I can use the event listener syntax on the prop! And this won't give me any errors… in fact, it works like a charm. Why? I can also add Can anyone send me a piece of the docs or explain this? I haven't found any references to this behavior. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The template attributes all get mapped down into VNode props (not to be confused with component props). The VNode props are the properties passed into the For There are a few other places in the docs that allude to this, but that's probably the most explicit. You can also see this using the Vue Playground to inspect the compiled output for a template with a listener. Inside the child component, any VNode prop can be turned into a component prop by listing it in Another case where this is relevant is fallthrough attributes. VNode props that aren't listed in either While it isn't documentation, also note the warning message in this example: This warning acknowledges that an emitted event can either be declared in the usual way (using |
Beta Was this translation helpful? Give feedback.
The template attributes all get mapped down into VNode props (not to be confused with component props). The VNode props are the properties passed into the
h()calls in a render function.For
v-on(or equivalently@), it'll be mapped onto a VNode prop prefixed withon. e.g.@clickbecomes the VNode proponClick. That part is documented at:There are a few other places in the docs that allude to this, but that's probably the most explicit. You can also see this using the Vue Playground to inspect the compiled output for a template with a listener.
Inside the child component, any VNode prop can be turned into a component prop by list…