其实注意两点就能很好实现:
1、循环中的el-form-item的props必须是唯一的【字符串】,主要是为了校验时找到相应的dom:
:prop="`answersList[${index}].answerTitle`"
2、循环中的el-form-item的rules要写,要指定规则,如果不在for循环里,则无需指定:
:rules="rules.answerTitle"
具体代码如下:
html:
<el-form-item v-for="(item, index) in form.answersList" :key="index" label="选项标题" :prop="`answersList[${index}].answerTitle`" :rules="rules.answerTitle"> <el-input v-model="item.answerTitle" placeholder="请输入选项标题" style="width: 100%;" /> </el-form-item>
js:
const form = reactive({ answersList: [{ answerTitle: '', imageUrl: '', isCorrect: '0', sort: '1' }] }) const rules = { answerTitle: [ { required: true, message: '请输入选项标题', trigger: 'blur' }, ], }