admin管理员组文章数量:1023764
Please see this minimum example, I have a simple ponent like this
HelloWorld.vue
<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
App.vue
<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
HelloWorld.vue
<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
App.vue
<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.
Share Improve this question asked Sep 10, 2020 at 15:10 JosephJoseph 4,7858 gold badges40 silver badges80 bronze badges 6- Bigger question, why are you typing the variable as a boolean but assigning a string to it? – Taplar Commented Sep 10, 2020 at 15:12
- It's a mon use case, say if you have an input type text element, it's default value might be an empty string, what if I want to render something once user fill out the input, this is one use case. – Joseph Commented Sep 10, 2020 at 15:15
-
That's pletely different. You have a
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 - If you are going to store actual string values in the element, and use the length of the string to derive the truthy state of it, then it should be typed as string and not boolean. – Taplar Commented Sep 10, 2020 at 15:17
- If it's really that not making sense, why Vue did not throw a warning about it? – Joseph Commented Sep 10, 2020 at 15:18
1 Answer
Reset to default 6Vuejs use Boolean props as in HTML, where empty string will be considered as true
They wrote it in their documentation
Please see this minimum example, I have a simple ponent like this
HelloWorld.vue
<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
App.vue
<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
HelloWorld.vue
<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
App.vue
<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.
Share Improve this question asked Sep 10, 2020 at 15:10 JosephJoseph 4,7858 gold badges40 silver badges80 bronze badges 6- Bigger question, why are you typing the variable as a boolean but assigning a string to it? – Taplar Commented Sep 10, 2020 at 15:12
- It's a mon use case, say if you have an input type text element, it's default value might be an empty string, what if I want to render something once user fill out the input, this is one use case. – Joseph Commented Sep 10, 2020 at 15:15
-
That's pletely different. You have a
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 - If you are going to store actual string values in the element, and use the length of the string to derive the truthy state of it, then it should be typed as string and not boolean. – Taplar Commented Sep 10, 2020 at 15:17
- If it's really that not making sense, why Vue did not throw a warning about it? – Joseph Commented Sep 10, 2020 at 15:18
1 Answer
Reset to default 6Vuejs use Boolean props as in HTML, where empty string will be considered as true
They wrote it in their documentation
本文标签: javascriptWhy passing empty string to Vue component Boolean props considered truthyStack Overflow
版权声明:本文标题:javascript - Why passing empty string to Vue component Boolean props considered truthy? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745605126a2158687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论