VUE 3:V模型和发射

发布于 2025-01-23 20:12:32 字数 1247 浏览 0 评论 0原文

我正在尝试使用v-model的VUE3双向套索,但是我的emit()不更新父值。你能告诉我我在哪里错了吗? 谢谢你!

父母看起来像:

<template>
  <div class="card">
    
    <LearnersTable
        v-model:toActivate="toActivate"
      />
      <!-- To control if this is being updated -->
      {{ toActivate.length }}


  </div>
</template>



<script setup>
[...]

// List of person to activate
const toActivate = [];

</script>

孩子(Learnertable)看起来像:

<template>
    [...]

    <tr v-for="row in rows" :key="row.id"  >
        <span>
            <Toggle v-model="row.enabled" @change="onChangeActivate(row)"/>
        </span>
    </tr>
    [...]

</template>

<script setup>
const props = defineProps({
  
  toActivate: {
    type: Array,
    default: () => [],
  },
});

const emit = defineEmits(['update:toActivate']);

const {
  toActivate,
} = toRefs(props);


function onChangeActivate(row) {

  if (row.enabled === true) {
    toActivate.value.push(row);
  }
  emit('update:toActivate', toActivate.value);
}

</script>

我在这里省略了一些代码。但是问题在于我的发射不起作用,我不会在父母中更新toactivate值。

谢谢 !

I'm trying to use Vue3 two-way biding with v-model, but my emit() doesn't update the parent value. Could you please tell me where I'm wrong?
Thank you!

Parent looks like:

<template>
  <div class="card">
    
    <LearnersTable
        v-model:toActivate="toActivate"
      />
      <!-- To control if this is being updated -->
      {{ toActivate.length }}


  </div>
</template>



<script setup>
[...]

// List of person to activate
const toActivate = [];

</script>

And Children (LearnersTable) looks like:

<template>
    [...]

    <tr v-for="row in rows" :key="row.id"  >
        <span>
            <Toggle v-model="row.enabled" @change="onChangeActivate(row)"/>
        </span>
    </tr>
    [...]

</template>

<script setup>
const props = defineProps({
  
  toActivate: {
    type: Array,
    default: () => [],
  },
});

const emit = defineEmits(['update:toActivate']);

const {
  toActivate,
} = toRefs(props);


function onChangeActivate(row) {

  if (row.enabled === true) {
    toActivate.value.push(row);
  }
  emit('update:toActivate', toActivate.value);
}

</script>

I'm omitting a little bit of code here. But the problem is that my emit doesn't work, I don't get the toActivate value updated in the parent.

Thank you !

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

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

发布评论

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

评论(1

别念他 2025-01-30 20:12:32

尝试使其被动

<script setup>
import { ref } from 'vue';

// List of person to activate
const toActivate = ref([]);

</script>

<LearnersTable
    v-model:to-activate="toActivate"
 />

Try to make it reactive:

<script setup>
import { ref } from 'vue';

// List of person to activate
const toActivate = ref([]);

</script>

and

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