Vue 3:v-model 并发出
Vue 3: v-model and emit
我正在尝试使用 Vue3 two-way 与 v-model
出价,但我的 emit()
没有更新 parent 值。你能告诉我哪里错了吗?
谢谢!
Parent 看起来像:
<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>
而 Children (LearnersTable) 看起来像:
<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>
我在这里省略了一些代码。但问题是我的发射不起作用,我没有在 parent.
中更新 toActivate
值
谢谢!
尝试使其具有反应性:
<script setup>
import { ref } from 'vue';
// List of person to activate
const toActivate = ref([]);
</script>
和
<LearnersTable
v-model:to-activate="toActivate"
/>
我正在尝试使用 Vue3 two-way 与 v-model
出价,但我的 emit()
没有更新 parent 值。你能告诉我哪里错了吗?
谢谢!
Parent 看起来像:
<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>
而 Children (LearnersTable) 看起来像:
<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>
我在这里省略了一些代码。但问题是我的发射不起作用,我没有在 parent.
中更新toActivate
值
谢谢!
尝试使其具有反应性:
<script setup>
import { ref } from 'vue';
// List of person to activate
const toActivate = ref([]);
</script>
和
<LearnersTable
v-model:to-activate="toActivate"
/>