VUE 3:V模型和发射
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使其被动
:
Try to make it reactive:
and