亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式(2)

 更新時(shí)間:2022年04月06日 14:56:18   作者:兮木兮木  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式,供大家參考,具體內(nèi)容如下

思路

  • 先把所有可能出現(xiàn)的表單/組件寫在主頁(yè)面
  • 每個(gè)表單/組件的slot 屬性值要與后端返回的表單/組件類型匹配
  • 根據(jù)后端返回的數(shù)據(jù),動(dòng)態(tài)生成一個(gè)slot列表,slot的name屬性要與數(shù)據(jù)的類型匹配,此列表放入一個(gè)子組件
  • 在主頁(yè)面引入子組件,把之前主頁(yè)面寫好的各個(gè)表單/組件放入子組件標(biāo)簽中,通過匹配slot插槽去渲染組件,沒有匹配到插槽的則不會(huì)渲染

案例

//主頁(yè)面
<template>
?? ?<el-form :model="formData">
? ? ? ? //把從后端取到的數(shù)據(jù)傳給子組件,動(dòng)態(tài)生成slot插槽列表
? ? ? <FormItemSlot :formItemList="formItemList">
? ? ? ? <el-form-item label="開關(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: '開關(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)文章

最新評(píng)論