输入CHECBOX V-MODEL不使用异步数据检查

发布于 2025-02-13 19:48:19 字数 392 浏览 0 评论 0原文

我有一个开始,从数据库中获取数据。它可以选择检查输入是否为true或false。但是问题是没有检查其何时为。我尝试了:检查并起作用,但是由于具有两种方式绑定的

代码:

<input type="checkbox" v-model="data">


const data = ref(false)

onBeforeMount(async () => {
    await data = fetchingData()
})

我没有编写孔代码,因此我需要V型模型,因为它在另一台计算机上的代码是从HEARD上写的。但是我的台词和v-Model没有检查。如果我使用:检查它的工作就像魅力一样,

也许这是一个新秀错误,但没有使用:检查:更改我没有看到任何解决方案。

I have a component that at the start of it, fetchs data from database. And it supose to check the input if the value is true or false. But the problem is that is not checking when its true. I tried :checked and it works, but i need the v-model because of the two way binding

code:

<input type="checkbox" v-model="data">


const data = ref(false)

onBeforeMount(async () => {
    await data = fetchingData()
})

I didnt write the hole code, because the code its on another computer, this was from head. But i am having poblems with v-model not checking. If i use :checked it works like a charm

Maybe this is a rookie mistake, but without using :checked with :change i am not seing any solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风轻花落早 2025-02-20 19:48:19

您应该使用data.value而不是数据进行反应性。为了演示目的,我将值直接分配为true。您可以用API呼叫代码替换。

实时演示

<script type="module" src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0-rc.5/vue.esm-browser.js"></script>
<div id="app">
  <input type="checkbox" v-model="data"/>
</div>

<script type="module">
import {ref, createApp, onBeforeMount } from 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0-rc.5/vue.esm-browser.js';

const app = createApp({
  setup() {
    let data = ref(false)
    onBeforeMount(() => {
      // data = true ❌
      data.value = true // ✅
    })
    return { data };
  }
});

app.mount('#app')
</script>

You should use data.value instead of data for the reactivity. For the demo purpose I am directly assign the value as true. You can replace that with the API call code.

Live Demo :

<script type="module" src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0-rc.5/vue.esm-browser.js"></script>
<div id="app">
  <input type="checkbox" v-model="data"/>
</div>

<script type="module">
import {ref, createApp, onBeforeMount } from 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0-rc.5/vue.esm-browser.js';

const app = createApp({
  setup() {
    let data = ref(false)
    onBeforeMount(() => {
      // data = true ❌
      data.value = true // ✅
    })
    return { data };
  }
});

app.mount('#app')
</script>

旧城空念 2025-02-20 19:48:19

我认为这不是复选框,但是您应该使用.value。写入ref 时。否则,您会失去反应性,而V模型将不起作用。

onBeforeMount(async () => {
    data.value = await fetchingData()
})  

希望这会有所帮助。

I do not think it is the checkbox, but that you should use the .value when you write to a ref. Otherwise you loose the reactivity and v-model will not work.

onBeforeMount(async () => {
    data.value = await fetchingData()
})  

Hope this helps.

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