vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式(2)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式,供大家參考,具體內(nèi)容如下
思路
- 先把所有可能出現(xiàn)的表單/組件寫(xiě)在主頁(yè)面
- 每個(gè)表單/組件的slot 屬性值要與后端返回的表單/組件類(lèi)型匹配
- 根據(jù)后端返回的數(shù)據(jù),動(dòng)態(tài)生成一個(gè)slot列表,slot的name屬性要與數(shù)據(jù)的類(lèi)型匹配,此列表放入一個(gè)子組件
- 在主頁(yè)面引入子組件,把之前主頁(yè)面寫(xiě)好的各個(gè)表單/組件放入子組件標(biāo)簽中,通過(guò)匹配slot插槽去渲染組件,沒(méi)有匹配到插槽的則不會(huì)渲染
案例
//主頁(yè)面 <template> ?? ?<el-form :model="formData"> ? ? ? ? //把從后端取到的數(shù)據(jù)傳給子組件,動(dòng)態(tài)生成slot插槽列表 ? ? ? <FormItemSlot :formItemList="formItemList"> ? ? ? ? <el-form-item label="開(kāi)關(guān)" slot="switch-component"> ? ? ? ? ? <el-switch ? ? ? ? ? ? v-model="formData.checked" ? ? ? ? ? ? active-color="#13ce66" ? ? ? ? ? ? inactive-color="#ff4949" ? ? ? ? ? > ? ? ? ? ? </el-switch> ? ? ? ? </el-form-item> ? ? ? ? <el-form-item label="名字" slot="input-component"> ? ? ? ? ? <el-input v-model="formData.username"></el-input> ? ? ? ? </el-form-item> ? ? ? ? <el-form-item label="角色" slot="select-component"> ? ? ? ? ? <el-select v-model="formData.role" placeholder="請(qǐng)選擇"> ? ? ? ? ? ? <el-option ? ? ? ? ? ? ? v-for="item in options" ? ? ? ? ? ? ? :key="item.value" ? ? ? ? ? ? ? :label="item.label" ? ? ? ? ? ? ? :value="item.label"> ? ? ? ? ? ? </el-option> ? ? ? ? ? </el-select> ? ? ? ? </el-form-item> ? ? ? </FormItemSlot> ? ? </el-form> </template> <script> ?? ?improt 引入slot列表子組件 FromItemSlot(下面有) ? ? export default { ? ? ? ? data() { ? ? ? return { ? ? ? ? ? ?? ? ? ? ? formItemList: [ ? ? ? ? ? { type: 'switch-component', require: true, label: '開(kāi)關(guān)', key: 'isOpen' }, ? ? ? ? ? { type: 'input-component', require: true, label: '姓名', key: 'name' }, ? ? ? ? ? { type: 'select-component', require: true, label: '角色', key: 'role' }, ? ? ? ? ], ? ? ? ? formData: { ? ? ? ? }, ? ? ? ? options: [ ? ? ? ? ? { ? ? ? ? ? ? value: '1', ? ? ? ? ? ? label: '李世民' ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? value: '2', ? ? ? ? ? ? label: '嬴政' ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? value: '3', ? ? ? ? ? ? label: '劉邦' ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? value: '4', ? ? ? ? ? ? label: '項(xiàng)羽' ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? value: '5', ? ? ? ? ? ? label: '范蠡' ? ? ? ? ? } ? ? ? ? ], ? ? ? } ? ? }, ? ? components: { ? ? ? FormItemSlot ? ? }, } </script>
//FormItemSlot.vue ?插槽列表 <template> ? <div class="slot-wrap"> ? ? <slot v-for="(item, index) in formItemList" :name="item.type"></slot> ? </div> </template> <script> ? export default { ? ? name: 'FormItemSlot', ? ? props: { ? ? ? formItemList: { ? ? ? ? type: Array, ? ? ? ? default() { ? ? ? ? ? return [] ? ? ? ? } ? ? ? } ? ? } ? } </script> <style scoped> </style>
以上數(shù)據(jù)會(huì)直接收集到formData中,此方式的好處在于表單數(shù)據(jù)都在一個(gè)頁(yè)面,方便數(shù)據(jù)處理.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
部署vue+Springboot前后端分離項(xiàng)目的步驟實(shí)現(xiàn)
這篇文章主要介紹了部署vue+Springboot前后端分離項(xiàng)目的步驟實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Vue3實(shí)現(xiàn)一個(gè)可左右滑動(dòng)操作組件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Vue3實(shí)現(xiàn)一個(gè)可左右滑動(dòng)操作組件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定幫助,感興趣的可以學(xué)一下2022-11-11vue 自動(dòng)化路由實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 自動(dòng)化路由實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09Vue3視頻播放器組件Vue3-video-play新手入門(mén)教程
這篇文章主要給大家介紹了關(guān)于Vue3視頻播放器組件Vue3-video-play新手入門(mén)教程的相關(guān)資料,本文實(shí)例為大家分享了vue-video-player視頻播放器的使用配置,供大家參考,需要的朋友可以參考下2023-12-12詳細(xì)聊聊前端如何實(shí)現(xiàn)token無(wú)感刷新(refresh_token)
實(shí)現(xiàn)token無(wú)感刷新對(duì)于前端來(lái)說(shuō)是一項(xiàng)非常常用的技術(shù),其本質(zhì)是為了優(yōu)化用戶(hù)體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于前端如何實(shí)現(xiàn)token無(wú)感刷新(refresh_token)的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue Element前端應(yīng)用開(kāi)發(fā)之根據(jù)ABP后端接口實(shí)現(xiàn)前端展示
本篇著重介紹基于ABP后端接口信息,實(shí)現(xiàn)對(duì)前端界面的開(kāi)發(fā)工作。2021-05-05