VUE 3“ V-Model”使用复选框,但数据是“ 1”。或“ 0”不是“ true”或“ false”

发布于 2025-02-05 19:38:39 字数 523 浏览 5 评论 0原文

由于后端的某些原因,他们使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他是夢罘是命 2025-02-12 19:38:39

从VUE 2升级到VUE 3,遇到了同样的问题。这无效:

true-value="1"
false-value="0"

但这有效:

:true-value="1"
:false-value="0"

Upgraded from Vue 2 to Vue 3, got same issue. This didn't work:

true-value="1"
false-value="0"

But this works:

:true-value="1"
:false-value="0"
却一份温柔 2025-02-12 19:38:39

因为静态HTML属性值是“字符串”类型。而且您期望“整数”。
因此,当您使用“ v-bind:”制作属性动态值时,您将发送'整数'

true-value="1" <-- string 
true-value="1" <-- integer
:true-value="'1'" <-- string

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'

true-value="1" <-- string 
true-value="1" <-- integer
:true-value="'1'" <-- string
清醇 2025-02-12 19:38:39

对于输入type =“复选框” <​​/code>,0将与false1相同> true

因此,settings.crawl_on = 1将与settings.crawl_on = true相同。

我使用typeScript以及JavaScript检查了相同的检查,并且在两种语言中都可以正常工作。

演示

new Vue({
  el: '#app',
  data: {
    settings: {
        crawl_on: 1
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input
         v-model="settings.crawl_on"
         type="checkbox"
         />
</div>

For input type="checkbox", 0 will be same as false and 1 will be same as true.

Hence, settings.crawl_on = 1 will be same as settings.crawl_on = true.

I checked same with TypeScript as well as JavaScript and it's working fine in both the languages.

Demo :

new Vue({
  el: '#app',
  data: {
    settings: {
        crawl_on: 1
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input
         v-model="settings.crawl_on"
         type="checkbox"
         />
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文