1. 問題
最近碰到一個(gè)問題,就是在 vue.js 項(xiàng)目中搭配 elementui 組件庫中的 el-form 動(dòng)態(tài)表單問題,踩了一點(diǎn)坑,在此記錄
<el-row
v-for="(item, index) in propertyGroup"
:key="index"
>
<el-row
class="basic-info-title"
type="flex"
justify="start"
align="middle"
>
<span class="green-line"></span>
<span>{{ item.groupName }}</span>
</el-row>
<el-row
type="flex"
justify="start"
align="middle"
style="margin-bottom: 12px; flex-wrap: wrap;"
>
<el-form-item
v-for="(list, j) in item.groupProperty"
:label="list.name"
:key="j"
:prop="
'propertyGroup.' + index + '.groupProperty.' + j + '.value'
"
:rules="{
required: list.unique,
message: `${list.name}不能為空`,
trigger: 'blur'
}"
>
<el-input
v-model="list.value"
v-if="list.type === 'text'"
class="common-input input-distance"
></el-input>
</el-form-item>
</el-row>
</el-row>
重點(diǎn)在第二層循環(huán) prop 綁定的問題, 'propertyGroup.' + index + '.groupProperty.' + j + '.value',是第一層和第二層的數(shù)組和 index 都要加上,要不然驗(yàn)證加不上。