Replies: 1 comment 1 reply
-
Comply with HTML, not JavaScript. CompB.vue: <script lang="ts" setup>
import CompA from './CompA.vue';
const props = withDefaults(
defineProps<{
foo?: boolean;
}>(),
{
foo: false,
},
);
</script>
<template>
<CompA v-bind="props" />
</template> |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The current behavior does not comply with JavaScript, for example:
CompA.vue:
CompB.vue:
App.vue:
What happend?
<CompB v-bind="{}" />
not equivalent to<CompB v-bind="{ foo: undefined }" />
<CompB />
not equivalent to<CompB :foo="undefined" />
If change the CompB props as follows (only given the deault value "undefined"), it‘s behavior will be changed.
This is difficult to understand from a JavaScript perspective, but in vue it is not a bug:
Under the current rules, if an optional boolean type prop is not given a default value, then
<Comp />
is equivalent to<Comp :foo="false" />
.More importantly, this can seriously hinder to wrap a component. If an optional boolean type prop is added to the wraped component and its default value is true while maintaining forward compatibility, it will cause the behavior of the component which include it to change.
In fact, it is common for us to use third-party component libraries and re wrap them.
If change boolean casting to the follows,perhaps more reasonable:
<Comp foo />
is equivalent to<Comp :foo="true" />
<Comp />
do nothing, equivalent to<Comp :foo="undefined" />
, If the Comp has default value, naturally, the final value is the default value. If the component expects false and omit prop,the prop should be defined as optional and given a default value of false.Beta Was this translation helpful? Give feedback.
All reactions