element中el-form-item屬性prop踩坑
最近負責前后端項目開發(fā),有個需求是實現(xiàn)Djangorestframework(drf)+element實現(xiàn)動態(tài)渲染form表單,drf后端提供json,前端從json中獲取form表單元素,并且綁定表單驗證規(guī)則
在el-form-item屬性prop上遇到報錯或者沒綁定到數(shù)據(jù),報錯如下
[Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'prop')"
element官方文檔解釋el-form-item的prop必須是el-form屬性model的直接子屬性
后端返回數(shù)據(jù)如下
{
"status": "success",
"code": 200,
"data": {
"form_attributes": {
"inline": true,
"label-width": "auto",
"size": "small"
},
"form_data": {
"name": null,
"path": null,
"component": null,
"hidden": false,
"meta": {
"icon": null,
"title": null
},
"pid": null
},
"form_item_list": [
{
"type": "text",
"prop": "name",
"label": "菜單名",
"placeholder": "請輸入菜單名",
"rules": [
{
"required": true,
"message": "請輸入菜單名",
"trigger": "blur"
}
]
},
{
"type": "text",
"prop": "path",
"label": "鏈接地址",
"placeholder": "'/'開頭",
"rules": [
{
"required": true,
"message": "'/'開頭",
"trigger": "blur"
}
]
},
{
"type": "text",
"prop": "component",
"label": "組件",
"placeholder": "參考前端組件填寫",
"rules": [
{
"required": true,
"message": "參考前端組件填寫",
"trigger": "blur"
}
]
},
{
"type": "switch",
"prop": "hidden",
"label": "是否隱藏",
"value": false
},
{
"type": "json",
"prop": "meta",
"item": [
{
"type": "text",
"prop": "icon",
"label": "圖標",
"placeholder": "圖標名字,參考前端圖標",
"rules": [
{
"required": true,
"message": "圖標名字,參考前端圖標",
"trigger": "blur"
}
]
},
{
"type": "text",
"prop": "title",
"label": "標題",
"placeholder": "請輸入菜單名",
"rules": [
{
"required": true,
"message": "請輸入菜單名",
"trigger": "blur"
}
]
}
]
},
{
"type": "select",
"prop": "pid",
"label": "父菜單",
"clearable": true,
"filterable": false,
"multiple": false,
"options": [
{
"label": "系統(tǒng)管理",
"value": 2
},
{
"label": "用戶管理",
"value": 3
},
{
"label": "菜單管理",
"value": 4
},
{
"label": "權(quán)限管理",
"value": 5
},
{
"label": "角色管理",
"value": 6
}
]
}
]
},
"message": null
}
從上面可以看到form表單元素對應(yīng)的是 form_item_list,表單提交數(shù)據(jù)是 form_data,這兩個是分開,也就是el-form中model綁定是 form_data 而el-form-item遍歷的是 form_item_list,注意 form_item_list 中含有嵌套類型json,對應(yīng)的后端是json字段渲染表單,單獨提供form_data返回字段設(shè)計是為了控制前端json內(nèi)容,動態(tài)字段中過于靈活,可以隨意修改json包含的字段和類型,所以返回字段由后端控制和校驗
截取前端渲染表單代碼如下,注意這個時候能渲染但是rules綁定是失敗的
<!-- 添加菜單對話框 -->
<el-dialog title="添加菜單" :visible.sync="addDialogVisible" width="50%" :close-on-click-modal="false" @close="addDialogClosed">
<!-- 表單內(nèi)容主體 -->
<el-form ref="addFormRef" :model="formData" :size="formAttributes.size" :inline="formAttributes.inline" :label-width="formAttributes.labelWidth">
<div v-for="(item, index) in formItemList" :key="index">
<el-form-item :prop="formItemList + index + item.prop" :label="item.label" :rules="item.rules">
<!-- text輸入框 -->
<el-input v-if="item.type==='text'" v-model="formData[item.prop]" clearable :placeholder="item.placeholder"></el-input>
<!-- textarea輸入框 -->
<el-input v-if="item.type==='textarea'" v-model="formData[item.prop]" clearable autosize :type="textarea" :placeholder="item.placeholder"></el-input>
<!-- 下拉框 -->
<el-select v-if="item.type==='select'" v-model="formData[item.prop]" clearable :multiple="item.multiple">
<el-option v-for="op in item.options" :key="op.value" :label="op.label" :value="op.value"></el-option>
</el-select>
<el-switch v-if="item.type==='switch'" v-model="formData[item.prop]" :label="item.label"></el-switch>
<template v-if="item.type==='json'">
<div v-for="(json_item, json_index) in item.item" :key="json_index">
<el-form-item :prop="item.item + json_index + json_item.prop" :label="json_item.label" :rules="json_item.rules" style="margin-left: -80px;">
<!-- text輸入框 -->
<el-input v-if="json_item.type==='text'" v-model="formData[item.prop][json_item.prop]" clearable :placeholder="json_item.placeholder"></el-input>
<!-- textarea輸入框 -->
<el-input v-if="json_item.type==='textarea'" v-model="formData[json_item.prop]" clearable autosize :type="textarea" :placeholder="json_item.placeholder"></el-input>
<!-- 下拉框 -->
<!-- <el-select v-if="item.type==='select'" v-model="formData[item.prop]" clearable :placeholder="item.label" :multiple="item.multiple" @change="item.change(formData[item.prop])"> -->
<el-select v-if="json_item.type==='select'" v-model="formData[json_item.prop]" clearable :multiple="json_item.multiple">
<el-option v-for="op in json_item.options" :key="op.value" :label="op.label" :value="op.value"></el-option>
</el-select>
<el-switch v-if="json_item.type==='switch'" v-model="formData[json_item.prop]" :label="json_item.label"></el-switch>
</el-form-item>
</div>
</template>
</el-form-item>
</div>
</el-form>
<!-- 底部 -->
<el-divider></el-divider>
<span slot="footer" class="dialog-footer">
<el-button @click="addDialogVisible = false">取消</el-button>
<el-button type="primary" @click="addForm">確定</el-button>
</span>
</el-dialog>
在前端渲染無法綁定到表單規(guī)則,如下圖

經(jīng)過研究,從上面的數(shù)據(jù)結(jié)構(gòu)可以發(fā)現(xiàn),form_data 中每個元素對應(yīng)是 form_item_list 中的 prop,那么有兩個寫法可以定位
第一種寫法如下,這種寫法沒那么直觀
<!-- 普通字段渲染 --> <el-form-item :prop="item.prop" :label="item.label" :rules="item.rules"> <!-- json字段渲染 --> <el-form-item :prop="item.prop + '.' + json_item.prop" :label="json_item.label" :rules="json_item.rules" style="margin-left: -80px;">
更好的第二種寫法如下
<el-form-item :prop="formItemList[index].prop" :label="item.label" :rules="item.rules"> <el-form-item :prop="formItemList[index].prop + '.' + item.item[json_index].prop" :label="json_item.label" :rules="json_item.rules" style="margin-left: -80px;">
此時能動態(tài)渲染表單和綁定表單項驗證規(guī)則,如下圖

總結(jié):上面兩種寫法都是為了定位到表單元素對應(yīng)的表單的model直接子屬性,踩坑的原因是遍歷表單的列表(v-for)和表單提交數(shù)據(jù)(model)不是在一個對象下,尤其是遍歷表單下,還包括嵌套表單json處理,需要注意定位json提交數(shù)據(jù)
到此這篇關(guān)于element中el-form-item屬性prop踩坑 的文章就介紹到這了,更多相關(guān)element prop屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+導航錨點聯(lián)動-滾動監(jiān)聽和點擊平滑滾動跳轉(zhuǎn)實例
今天小編就為大家分享一篇vue+導航錨點聯(lián)動-滾動監(jiān)聽和點擊平滑滾動跳轉(zhuǎn)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子
今天小編就為大家分享一篇Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue解決element-ui消息提示$message重疊問題
這篇文章主要為大家介紹了Vue解決element-ui消息提示$message重疊問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
element?ui富文本編輯器的使用效果與步驟(quill-editor)
富文本編輯器在任何項目中都會用到,在Element中我們推薦vue-quill-editor組件,下面這篇文章主要給大家介紹了關(guān)于element?ui富文本編輯器的使用效果與步驟(quill-editor)的相關(guān)資料,需要的朋友可以參考下2022-10-10
Vue3+TS實現(xiàn)動態(tài)路由權(quán)限的示例詳解
當我們在開發(fā)一個大型的前端應(yīng)用時,動態(tài)路由權(quán)限是一個必不可少的功能,本文將介紹如何使用Vue 3和TypeScript來實現(xiàn)動態(tài)路由權(quán)限,希望對大家有所幫助2024-01-01

