Please see this minimum example, I have a simple ponent like this
<template>
<div v-if="show">I will show even if show prop is passed as empty string</div>
</template>
<script>
export default {
props: {
show: Boolean,
},
};
</script>
Now, If I do this to that ponent
<template>
<div>
<HelloWorld :show="show" />
</div>
</template>
<script>
import HelloWorld from "./ponents/HelloWorld";
export default {
data() {
return {
show: "",
};
},
ponents: {
HelloWorld,
},
};
</script>
It will render out that string!
Why is this happening?
An empty string is considered falsy but is truthy in the Vue ponent, that's quite weird.
I probably get why it does this way because now you are enabled to do this
<template>
<div>
<!-- you can just write show -->
<HelloWorld show />
</div>
</template>
However, I think the template-piler should do this thing because take a look at JSX
It supports the shorthand at piler level, so I was wondering why Vue design like this.
Please see this minimum example, I have a simple ponent like this
<template>
<div v-if="show">I will show even if show prop is passed as empty string</div>
</template>
<script>
export default {
props: {
show: Boolean,
},
};
</script>
Now, If I do this to that ponent
<template>
<div>
<HelloWorld :show="show" />
</div>
</template>
<script>
import HelloWorld from "./ponents/HelloWorld";
export default {
data() {
return {
show: "",
};
},
ponents: {
HelloWorld,
},
};
</script>
It will render out that string!
Why is this happening?
An empty string is considered falsy but is truthy in the Vue ponent, that's quite weird.
I probably get why it does this way because now you are enabled to do this
<template>
<div>
<!-- you can just write show -->
<HelloWorld show />
</div>
</template>
However, I think the template-piler should do this thing because take a look at JSX
It supports the shorthand at piler level, so I was wondering why Vue design like this.
show
variable here that you are typing as a boolean with the intention of setting it to true if it should show, and false if it should not. Setting it as a non-boolean doesn't make sense.
– Taplar
Commented
Sep 10, 2020 at 15:16
Vuejs use Boolean props as in HTML, where empty string will be considered as true
They wrote it in their documentation