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)目中,我們就可以直接使用封裝好的組件,而無(wú)需重復(fù)開發(fā)。
預(yù)期效果
預(yù)期效果如下。

創(chuàng)建表單組件
先把架子搭起來(lái),組件名為H3yunFormCompV1,這個(gè)是隨便取的哈。然后隨便在哪個(gè)地方用上,方便測(cè)試。
<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}非常的簡(jiǎn)單。
<H3yunFormCompV1 :formData="formData"></H3yunFormCompV1>
data() {
return {
formData: []
}
}子組件如下:使用v-for遍歷formData,并把label和value取出來(lái)。
<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ù)測(cè)的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)表格翻頁(yè)的實(shí)例
- Vue2.0 UI框架ElementUI使用方法詳解
相關(guān)文章
el-select自定義指令實(shí)現(xiàn)觸底加載分頁(yè)請(qǐng)求options數(shù)據(jù)(完整代碼和接口可直接用)
某些情況下,下拉框需要做觸底加載,發(fā)請(qǐng)求,獲取option的數(shù)據(jù),下面給大家分享el-select自定義指令實(shí)現(xiàn)觸底加載分頁(yè)請(qǐng)求options數(shù)據(jù)(附上完整代碼和接口可直接用),感興趣的朋友參考下吧2024-02-02
Vue使用extend動(dòng)態(tài)創(chuàng)建組件的實(shí)現(xiàn)
本文主要介紹了Vue使用extend動(dòng)態(tài)創(chuàng)建組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue項(xiàng)目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案
這篇文章主要介紹了vue項(xiàng)目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
分享一個(gè)vue項(xiàng)目“腳手架”項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要介紹了分享一個(gè)vue項(xiàng)目“腳手架”項(xiàng)目的實(shí)現(xiàn)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-05-05
Vue實(shí)例中el和data的兩種寫法小結(jié)
這篇文章主要介紹了Vue實(shí)例中el和data的兩種寫法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
vuecli+AXdownload下載組件封裝?+css3下載懸浮球動(dòng)畫效果
當(dāng)觸發(fā)下載功能的時(shí)候,會(huì)觸發(fā)一個(gè)下載動(dòng)畫,下載懸浮球會(huì)自動(dòng)彈出,并且閃爍提示有新的下載任務(wù)直到下載任務(wù)完場(chǎng)提示,接下來(lái)通過本文介紹vuecli+AXdownload下載組件封裝?+css3下載懸浮球動(dòng)畫效果,需要的朋友可以參考下2024-05-05

