Vue3通過JSON渲染ElementPlus表單的流程步驟
1、引言
在公司里遇到這樣一個(gè)需求:要做產(chǎn)品管理模塊,內(nèi)容不復(fù)雜,只是用Vue+Element寫表單,但是,這個(gè)功能里的屬性非常多,可能還涉及很多復(fù)雜且重復(fù)的業(yè)務(wù)。
按照常規(guī)開發(fā)經(jīng)驗(yàn),很簡(jiǎn)單,寫好一個(gè)item,剩下一路Ctrl+C ...
但作為一個(gè)優(yōu)秀的牛碼人,總有更高級(jí)的辦法實(shí)現(xiàn): 使用JSON渲染Element表單元素
技術(shù)棧:Vue3、ElementPlus
2、建立環(huán)境
首先,要有一個(gè)vue3項(xiàng)目,包含element-plus庫(kù)
這里我使用了vite官網(wǎng)提供的vue項(xiàng)目創(chuàng)建方式:開始 | Vite 官方中文文檔 (vitejs.cn)
創(chuàng)建完成后,用yarn添加了element-plus包

修改src/main.js,引入element-plus庫(kù)
// main.js
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
const app = createApp(App)
// 引入element-plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
app.use(ElementPlus)
app.mount('#app')
3、創(chuàng)建組件
在src/components目錄下,無用組件HelloWorld.vue可以刪除,新建目錄ConfigForm,包含一個(gè)index.vue文件

4、構(gòu)建基本功能
給組件<ConfigForm />寫入內(nèi)容
<!-- components/ConfigForm/index.vue -->
<script setup>
defineProps({
modelValue: Object, // 綁定對(duì)象
config: Object // 組件配置 config內(nèi)columns是需要渲染的表單元素
})
</script>
<template>
<h3>配置式表單</h3>
<!-- 表單框架 -->
<el-form :model="modelValue" label-width="auto" style="max-width: 600px">
<!-- 循環(huán)config.columns,使用 <el-form-item /> 渲染表單元素 -->
<template v-for="(item, index) in config.columns" :key="item.prop">
<el-form-item :label="item.label" :prop="item.prop">
<!-- 通過type判斷是哪一種表單元素,v-bind繼承表單元素參數(shù) -->
<el-input v-if="item.type === 'input'" v-model="modelValue[item.prop]" v-bind="item.props" />
<el-select v-else-if="item.type === 'select'" v-model="modelValue[item.prop]" v-bind="item.props">
<el-option v-for="o in item.options" :key="o.value" :label="o.label" :value="o.value" />
</el-select>
</el-form-item>
</template>
</el-form>
</template>
找一個(gè)頁(yè)面使用它
為了方便演示,我使用了APP.vue
<!-- APP.vue -->
<script setup>
import { ref } from 'vue'
import ConfigForm from './components/ConfigForm/index.vue' // 引入組件
/**ConfigForm綁定對(duì)象 */
const formData = ref({})
/**ConfigForm配置 */
const config = ref({
columns: [
{
prop: 'name',
label: '姓名',
type: 'input',
props: { clearable: true, placeholder: '請(qǐng)輸入姓名' }, // 繼承到 <el-input /> 的props
},
{
prop: 'sex',
label: '性別',
value: '1',
type: 'select',
options: [{ label: '男', value: 'man' }, { label: '女', value: 'woman' }],
},
]
})
</script>
<template>
<!-- 使用 <ConfigForm /> -->
<ConfigForm v-model="formData" :config="config" />
<div>
<b>綁定數(shù)據(jù):</b>
<div v-for="(value, key) in formData" style="display: grid; grid-template-columns: 100px 1fr 200px;">
<b>{{ key }}</b>
<b>:</b>
<span>{{ value }}</span>
</div>
</div>
</template>
這樣就達(dá)成了template式轉(zhuǎn)JSON式的目的
效果圖:

5、必要擴(kuò)展:校驗(yàn)
既然是表單,那么肯定少不了驗(yàn)證
這里很簡(jiǎn)單,只需要在<el-form-item />標(biāo)簽上綁定rules就可以生效
<!-- components/ConfigForm/index.vue -->
<script setup>
+ import { ref } from 'vue'
... 其他代碼
+ const mainRef = ref(null)
+ defineExpose({ mainRef }) // 暴露mainRef給父組件使用
</script>
<template>
<h3>配置式表單</h3>
<!-- 表單框架 -->
- <el-form :model="modelValue" label-width="auto" style="max-width: 600px">
+ <el-form :model="modelValue" label-width="auto" style="max-width: 600px" ref="mainRef">
<!-- 循環(huán)config.columns,使用 <el-form-item /> 渲染表單元素 -->
<template v-for="(item, index) in config.columns" :key="item.prop">
- <el-form-item :label="item.label" :prop="item.prop">
+ <el-form-item :label="item.label" :prop="item.prop" :rules="item.rules">
... 其他代碼
</el-form-item>
</template>
</el-form>
</template>
然后再APP.vue中,給config.columns的項(xiàng)增加rules屬性
<!-- APP.vue -->
<script setup>
+ import { ElMessage } from 'element-plus'
... 其他代碼
/**ConfigForm配置 */
const config = ref({
columns: [
{
prop: 'name',
label: '姓名',
type: 'input',
props: { clearable: true, placeholder: '請(qǐng)輸入姓名' }, // 繼承到 <el-input /> 的props
+ rules: [{ required: true, message: '請(qǐng)輸入姓名', trigger: 'blur' }], // 此元素的校驗(yàn)規(guī)則
},
... 其他代碼
]
})
+ const configFormRef = ref()
+ /**提交表單 */
+ const onSubmit = async () => {
+ let submitFlag = true
+ // 校驗(yàn)
+ const valid = await configFormRef.value.mainRef.validate().catch((err) => err)
+ if (valid !== true) {
+ submitFlag = false
+ }
+ if (submitFlag) {
+ ElMessage.success('校驗(yàn)成功')
+ } else {
+ ElMessage.error('校驗(yàn)失敗')
+ }
+ }
</script>
<template>
<!-- 使用 <ConfigForm /> -->
- <ConfigForm v-model="formData" :config="config" />
+ <ConfigForm v-model="formData" :config="config" ref="configFormRef" />
+ <el-button type="primary" @click="onSubmit()">校驗(yàn)并提交</el-button>
... 其他代碼
</template>
這樣設(shè)定的rules就生效了,而且可以使用onSubmit方法驗(yàn)證整個(gè)表單

6、技術(shù)點(diǎn)
- 父子傳參(defineProps)
- 數(shù)據(jù)綁定(v-model、v-bind)
- 循環(huán)處理(v-for、v-if、v-else)
詳細(xì)功能可以在網(wǎng)上查閱
以上就是Vue3通過JSON渲染ElementPlus表單的流程步驟的詳細(xì)內(nèi)容,更多關(guān)于Vue3 JSON渲染ElementPlus的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Vue3+Vite+ElementPlus構(gòu)建學(xué)習(xí)筆記
- Vue3+ElementPlus封裝圖片空間組件的門面實(shí)例
- vue3+js+elementPlus使用富文本編輯器@vueup/vue-quill詳細(xì)教程
- vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程
- vue3+elementplus 樹節(jié)點(diǎn)過濾功能實(shí)現(xiàn)
- 如何在Vue3中正確使用ElementPlus,親測(cè)有效,避坑
- vue3+elementplus前端生成圖片驗(yàn)證碼完整代碼舉例
- Vue3 + ElementPlus動(dòng)態(tài)合并數(shù)據(jù)相同的單元格的完整代碼
相關(guān)文章
vue實(shí)現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對(duì)應(yīng)的打包命令)
這篇文章主要介紹了vue實(shí)現(xiàn)分環(huán)境打包步驟(給不同的環(huán)境配置相對(duì)應(yīng)的打包命令),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
基于Vue2-Calendar改進(jìn)的日歷組件(含中文使用說明)
這篇文章主要介紹了基于Vue2-Calendar改進(jìn)的日歷組件(含中文使用說明)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04
vue + Electron 制作桌面應(yīng)用的示例代碼
這篇文章主要介紹了vue + Electron 制作桌面應(yīng)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
淺談Vue3.0新版API之composition-api入坑指南
這篇文章主要介紹了Vue3.0新版API之composition-api入坑指南,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Vue模擬響應(yīng)式原理底層代碼實(shí)現(xiàn)的示例
最近去面試的人都會(huì)有這個(gè)體會(huì),去年面試官只問我怎么用vue,今年開始問我vue響應(yīng)式原理,本文就詳細(xì)的介紹一下2021-08-08

