Vue3,响应式双向绑定失败。
reactive创建一个对象twoObject ,对象中存在一个集合list。for循环表单时,list中的某个对象的属性score无法双向绑定,DOM上输入后,并未更新到js
<script lang="ts" setup>
const twoObject = reactive({
list: [
{
id: 1,
name: "单选1",
score: 0,
},
{
id: 2,
name: "单选2",
score: 0,
}
]
})
const submit = () => {
console.log(oneList)
}
function setFormRef(el: any, index: number) {
console.log(el)
if (el) {
formRef.value[index] = el;
}
}
</script>
<template>
<a-card class="questionnaire-item-card">
<a-form :model="twoObject" style="width: 100%" layout="inline">
<div v-for="(option, index) in twoObject.list" :key="option.id" class="questionnaire-option-item">
<a-form-item :ref="(el: any) => setFormRef(el, index)" style="display: inline"
:name="['twoList', index, 'score']" :label="option.name"
:rules="[{ required: true, message: '请填写对应选项分数', trigger: 'change' }]">
<a-input-number v-model:value="option.score" :precision="0" allow-clear placeholder="请输入" />
</a-form-item>
</div>
</a-form>
<a-button @click="submit">保存</a-button>
</a-card>
</template>