vue實現動態(tài)表單動態(tài)渲染組件的方式(2)
更新時間:2022年04月06日 14:56:18 作者:兮木兮木
這篇文章主要為大家詳細介紹了vue實現動態(tài)表單動態(tài)渲染組件的方式第二篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現動態(tài)表單動態(tài)渲染組件的方式,供大家參考,具體內容如下
思路
- 先把所有可能出現的表單/組件寫在主頁面
- 每個表單/組件的slot 屬性值要與后端返回的表單/組件類型匹配
- 根據后端返回的數據,動態(tài)生成一個slot列表,slot的name屬性要與數據的類型匹配,此列表放入一個子組件
- 在主頁面引入子組件,把之前主頁面寫好的各個表單/組件放入子組件標簽中,通過匹配slot插槽去渲染組件,沒有匹配到插槽的則不會渲染
案例
//主頁面 <template> ?? ?<el-form :model="formData"> ? ? ? ? //把從后端取到的數據傳給子組件,動態(tài)生成slot插槽列表 ? ? ? <FormItemSlot :formItemList="formItemList"> ? ? ? ? <el-form-item label="開關" 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="請選擇"> ? ? ? ? ? ? <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: '開關', 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: '項羽' ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? 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>
以上數據會直接收集到formData中,此方式的好處在于表單數據都在一個頁面,方便數據處理.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue3視頻播放器組件Vue3-video-play新手入門教程
這篇文章主要給大家介紹了關于Vue3視頻播放器組件Vue3-video-play新手入門教程的相關資料,本文實例為大家分享了vue-video-player視頻播放器的使用配置,供大家參考,需要的朋友可以參考下2023-12-12詳細聊聊前端如何實現token無感刷新(refresh_token)
實現token無感刷新對于前端來說是一項非常常用的技術,其本質是為了優(yōu)化用戶體驗,下面這篇文章主要給大家介紹了關于前端如何實現token無感刷新(refresh_token)的相關資料,需要的朋友可以參考下2022-11-11Vue Element前端應用開發(fā)之根據ABP后端接口實現前端展示
本篇著重介紹基于ABP后端接口信息,實現對前端界面的開發(fā)工作。2021-05-05