有个布尔类型的组件属性modelValue,当给这个属性传递空字符串”“的时候,组件接收的值是true,我跟踪代码发现了这个地方,请问可以给解答一下这里为什么要转换成true吗?谢谢 #13129
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Boolean props aim to behave like boolean attributes in HTML. So, for example, the <input type="checkbox" checked>
<input type="checkbox" checked="">
<input type="checkbox" checked="checked">Vue handles boolean component props the same way. Imagine we have the prop <MyCheckbox checked />
<MyCheckbox checked="" />
<MyCheckbox checked="checked" />All of these will lead to the If you have a prop that can be either a string or a boolean then the order matters. For example, |
Beta Was this translation helpful? Give feedback.
-
|
为了兼容dom中只用属性的这种写法 |
Beta Was this translation helpful? Give feedback.

Boolean props aim to behave like boolean attributes in HTML.
So, for example, the
checkedattribute of a native checkbox can be set in various ways:Vue handles boolean component props the same way. Imagine we have the prop
checked: Boolean, then:All of these will lead to the
checkedprop being set totrue. The first two will both lead tocheckedbeing an empty string.If you have a prop that can be either a string or a boolean then the order matters. For example,
checked: [String, Boolean]would not…