Vue2+ElementUI表單、Form組件的封裝過程
Vue2+ElementUI表單、Form組件的封裝 :引言
在 Vue2 項(xiàng)目中,ElementUI 的 el-form 組件是常用的表單組件。它提供了豐富的功能和樣式,可以滿足各種需求。但是,在實(shí)際開發(fā)中,我們經(jīng)常會(huì)遇到一些重復(fù)性的需求,比如:
- 需要對(duì)表單進(jìn)行校驗(yàn)
- 需要對(duì)表單數(shù)據(jù)進(jìn)行重置
- 需要在表單中添加額外的功能,比如動(dòng)態(tài)添加表單項(xiàng)等
為了提高開發(fā)效率,我們可以對(duì) el-form 組件進(jìn)行封裝,將這些重復(fù)性的需求抽象成通用的功能。這樣,在后續(xù)的項(xiàng)目中,我們就可以直接使用封裝好的組件,而無需重復(fù)開發(fā)。
預(yù)期效果
預(yù)期效果如下。

創(chuàng)建表單組件
先把架子搭起來,組件名為H3yunFormCompV1,這個(gè)是隨便取的哈。然后隨便在哪個(gè)地方用上,方便測試。
<template>
<div>
<el-form>
</el-form>
</div>
</template>
<script>
export default {
name: "H3yunFormCompV1",
data() {
return {}
},
props: {},
mounted() {
},
methods: {}
}
</script>
<style scoped>
</style>父組件傳遞表單數(shù)據(jù),子組件遍歷數(shù)據(jù)
把formData數(shù)據(jù)傳遞過去。formData是一個(gè)列表,每個(gè)對(duì)象的結(jié)果如下{label: null, value: null}非常的簡單。
<H3yunFormCompV1 :formData="formData"></H3yunFormCompV1>
data() {
return {
formData: []
}
}子組件如下:使用v-for遍歷formData,并把label和value取出來。
<template>
<div>
<el-form ref="form" :model="form" :inline="true">
<el-form-item v-for="(item,key) in formData" :key="key" :label="item.label">
<el-input v-model="item.value"></el-input>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: "H3yunFormCompV1",
data() {
return {
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
}
}
},
props: {
formData: Array
},
mounted() {
},
methods: {}
}
</script>
<style scoped>
</style>添加disabled屬性
子組件的完整代碼
<template>
<div>
<el-row>
<el-form
ref="form"
:model="form"
label-position="top"
size="small"
>
<el-col :span="6" v-for="(item,key) in formData" :key="key">
<div class="box">
<el-form-item :label="item.label">
<el-input v-model="item.value" :disabled="item.disabled"></el-input>
</el-form-item>
</div>
</el-col>
</el-form>
</el-row>
</div>
</template>
<script>
export default {
name: "H3yunFormCompV1",
data() {
return {
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
}
}
},
props: {
formData: Array
},
mounted() {
},
methods: {}
}
</script>
<style>
.box {
font-size: 14px;
padding: 6px 12px;
color: #304265;
background: #f8fafc;
border-radius: 4px;
min-height: 22px;
box-sizing: content-box;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
}
.box .el-form-item__label {
position: relative;
font-size: 14px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #304265;
font-weight: 400 !important;
}
</style>效果如下:

適配JSON數(shù)據(jù)
先看效果,下面是銷量預(yù)測的json數(shù)據(jù)。

代碼如下:主要是把json數(shù)據(jù)解析為了一個(gè)個(gè)表單項(xiàng)。
<template>
<div>
<el-row>
<el-form
ref="form"
:model="form"
label-position="top"
size="small"
>
<el-col :span="6" v-for="(item,key) in formStructure" :key="key">
<div class="box">
<el-form-item :label="item.label">
<el-input v-model="item.value" :disabled="item.disabled"></el-input>
</el-form-item>
</div>
</el-col>
</el-form>
</el-row>
</div>
</template>
<script>
export default {
name: "H3yunFormCompV1",
data() {
return {
form: {},
formStructure: []
}
},
props: {
formData: Array
},
watch: {
// 監(jiān)控父組件的表單數(shù)據(jù)
formData: {
handler(newFormData) {
// 當(dāng) formData 變化時(shí)執(zhí)行的操作
// 解析新表單結(jié)構(gòu)
this.parseFormStructure(newFormData);
},
deep: true, // 深度監(jiān)聽,用于監(jiān)聽數(shù)組或?qū)ο髢?nèi)部的變化
},
},
mounted() {
// 解析新表單結(jié)構(gòu) - 第一次點(diǎn)擊時(shí)執(zhí)行
this.parseFormStructure()
},
methods: {
// 解析表單結(jié)構(gòu)
parseFormStructure() {
// 清除表單結(jié)構(gòu)和表單數(shù)據(jù)
this.formStructure = []
this.form = {}
const formStructure = []
// column的數(shù)據(jù)類型:{ label: null, value: null, prop: null, disabled: null, dataType: null}
this.formData.forEach(column => {
if (column.dataType == undefined) {
column.dataType = 'text'
}
// 如果數(shù)據(jù)是json,需要把JSON數(shù)據(jù)裝為column的結(jié)構(gòu)
if (column.dataType == 'json') {
const label = column.label
const prop = column.prop
const jsonValue = column.value
const disabled = column.disabled
const jsonObj = JSON.parse(jsonValue)
// 構(gòu)建column對(duì)象
Object.keys(jsonObj).forEach(key => {
const childLabel = `${label}.${key}`
const childProp = `${prop}.${key}`
const childValue = jsonObj[key]
const childDisabled = disabled
const childColumn = {
label: childLabel,
value: childValue,
prop: childProp,
disabled: childDisabled,
dataType: 'text'
}
formStructure.push(childColumn)
})
} else {
formStructure.push(column)
}
})
this.formStructure = formStructure
}
}
}
</script>
<style>
.box {
font-size: 14px;
padding: 6px 12px;
color: #304265;
background: #f8fafc;
border-radius: 4px;
min-height: 22px;
box-sizing: content-box;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
}
.box .el-form-item__label {
position: relative;
font-size: 14px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #304265;
font-weight: 400 !important;
}
</style>表單提交
首先寫個(gè)提交按鈕,并添加個(gè)change表單提交事件。
<el-button type="primary" size="small" @change="submitForm">提交</el-button>
提交邏輯如下
// 提交的數(shù)據(jù)在this.form里面,表單驗(yàn)證通過后可以使用axios把表單數(shù)據(jù)提交到服務(wù)器。
submitForm() {
// 使用 this.$refs.form.validate() 進(jìn)行表單驗(yàn)證
this.$refs.form.validate((valid) => {
if (valid) {
// 表單驗(yàn)證通過,執(zhí)行提交操作
// 在這里你可以使用 Axios 或其他方式提交數(shù)據(jù)到后端
// 示例:假設(shè)有一個(gè)名為 submitData 的方法用于提交數(shù)據(jù)
this.submitData();
} else {
// 表單驗(yàn)證失敗,可以做一些處理,如提示用戶
this.$message.error('表單驗(yàn)證失敗,請(qǐng)檢查輸入信息。');
}
});
},
submitData() {
// 在這里執(zhí)行提交數(shù)據(jù)的操作
// 可以使用 Axios 或其他方式發(fā)送數(shù)據(jù)到后端
// 示例:假設(shè)有一個(gè)名為 postData 的方法用于發(fā)送數(shù)據(jù)
// postData(this.form)
this.$message.success('表單提交成功!');
}到此這篇關(guān)于Vue2+ElementUI表單、Form組件的封裝的文章就介紹到這了,更多相關(guān)Vue2 ElementUI表單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決vue2中使用elementUi打包報(bào)錯(cuò)的問題
- VUE2.0+ElementUI2.0表格el-table實(shí)現(xiàn)表頭擴(kuò)展el-tooltip
- VUE2.0+ElementUI2.0表格el-table循環(huán)動(dòng)態(tài)列渲染的寫法詳解
- VUE2.0 ElementUI2.0表格el-table自適應(yīng)高度的實(shí)現(xiàn)方法
- vue2.0 elementUI制作面包屑導(dǎo)航欄
- Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁的實(shí)例
- Vue2.0 UI框架ElementUI使用方法詳解
相關(guān)文章
vue3成功創(chuàng)建項(xiàng)目后?run?serve啟動(dòng)項(xiàng)目報(bào)錯(cuò)的解決
這篇文章主要介紹了vue3成功創(chuàng)建項(xiàng)目后?run?serve啟動(dòng)項(xiàng)目報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動(dòng)態(tài)合并方式
這篇文章主要介紹了vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動(dòng)態(tài)合并方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
如何使用vue-pdf-embed實(shí)現(xiàn)PDF在線預(yù)覽
vue-pdf-embed是一個(gè)基于Vue.js的插件,專門用于在Vue應(yīng)用中嵌入和展示PDF文件,本文將使用vue-pdf-embed實(shí)現(xiàn)PDF在線預(yù)覽功能,有需要的小伙伴可以參考一下2025-03-03
前端vue實(shí)現(xiàn)的h5頁面接入微信支付流程(jsapi方式)
vue實(shí)現(xiàn)微信支付有三種方式,第一種方式是PC端支付,第二種方式是H5支付,第三種方式是微信公眾號(hào)支付,這篇文章主要給大家介紹了關(guān)于前端vue實(shí)現(xiàn)的h5頁面接入微信支付流程,文中介紹的方法是利用jsapi方式,通過代碼將實(shí)現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
在Vue項(xiàng)目中引入騰訊驗(yàn)證碼服務(wù)的教程
這篇文章主要介紹了在Vue項(xiàng)目中引入騰訊驗(yàn)證碼服務(wù)的教程,需要的朋友可以參考下2018-04-04
Vue2.0基于vue-cli+webpack同級(jí)組件之間的通信教程(推薦)
下面小編就為大家?guī)硪黄猇ue2.0基于vue-cli+webpack同級(jí)組件之間的通信教程(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
vue系列之動(dòng)態(tài)路由詳解【原創(chuàng)】
下面小編就為大家?guī)硪黄獀ue系列之動(dòng)態(tài)路由詳解【原創(chuàng)】。小編覺得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Vue reactive函數(shù)實(shí)現(xiàn)流程詳解
一個(gè)基本類型的數(shù)據(jù),想要變成響應(yīng)式數(shù)據(jù),那么需要通過ref函數(shù)包裹,而如果是一個(gè)對(duì)象的話,那么需要使用reactive函數(shù),這篇文章主要介紹了Vue reactive函數(shù)2023-01-01
Vue3實(shí)現(xiàn)clipboard復(fù)制的使用示例
在日常開發(fā)中,為用戶提供復(fù)制文本功能的需求比較常見,下面介紹一款vue3可用的插件,可以快速實(shí)現(xiàn)這個(gè)功能,感興趣的可以了解一下2023-11-11

