VUE 3“ V-Model”使用复选框,但数据是“ 1”。或“ 0”不是“ true”或“ false”
由于后端的某些原因,他们使用0或1,而不是false或true的布尔人。
因此,当我尝试使用来自API的布尔数据时,TS抱怨:
// settings.crawl_on outputs 0 or 1
<input
v-model=“settings.crawl_on”
type="checkbox"
/>
我尝试添加以下代码也不起作用:
true-value="1"
false-value="0"
TS说:
(属性)inputhtmlattributes.checked?:not [] |设置|布鲁利亚人 键入'number'不能分配给'任何[] |设置| booleanish'.ts(2322)Runtime-dom.d.t.ts(629,3):预期类型来自属性“检查”,该属性在类型上在此处声明 'ElementAttrs'
是否有一种方法可以覆盖此问题,或者有什么正确的用途?
Because of some reason on the backend, they use 0 or 1 and not false or true for booleans.
So, when I try to use the boolean data from API, TS complains:
// settings.crawl_on outputs 0 or 1
<input
v-model=“settings.crawl_on”
type="checkbox"
/>
I tried adding the below code it doesn't work either:
true-value="1"
false-value="0"
TS says:
(property) InputHTMLAttributes.checked?: any[] | Set | Booleanish
Type 'number' is not assignable to type 'any[] | Set |
Booleanish'.ts(2322)runtime-dom.d.ts(629, 3): The expected type comes from property 'checked' which is declared here on type
'ElementAttrs'
Is there a way to override this or what is the correct use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从VUE 2升级到VUE 3,遇到了同样的问题。这无效:
但这有效:
Upgraded from Vue 2 to Vue 3, got same issue. This didn't work:
But this works:
因为静态HTML属性值是“字符串”类型。而且您期望“整数”。
因此,当您使用“ v-bind:”制作属性动态值时,您将发送'整数'
Because static html attributes values are 'String' type. And you are expecting 'Integers'.
So, when you use "v-bind:" making attributes dynamic values, you are sending 'Integers'
对于输入
type =“复选框” </code>,
。0
将与false
和1
相同> true因此,
settings.crawl_on = 1
将与settings.crawl_on = true
相同。我使用
typeScript
以及JavaScript
检查了相同的检查,并且在两种语言中都可以正常工作。演示:
For input
type="checkbox"
,0
will be same asfalse
and1
will be same astrue
.Hence,
settings.crawl_on = 1
will be same assettings.crawl_on = true
.I checked same with
TypeScript
as well asJavaScript
and it's working fine in both the languages.Demo :