vue实现动态表单动态渲染组件的方式(1)

 更新时间:2022年04月06日 15:00:16   作者:兮木兮木  
这篇文章主要为大家详细介绍了vue实现动态表单动态渲染组件的方式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

vue 实现动态表单/动态渲染组件的方式(一),供大家参考,具体内容如下

思路

  • 先写好各个可能会出现的表单或者自定义的组件,引入。
  • 此时后端可能会给到一个对象型数组,每个对象有要渲染组件的一个类型标识
  • 利用component is 动态组件,根据不同的组件类型标识,渲染展示不同的组件
  • 在利用组件的数据通信去收集各个子组件的数据

实现demo

三个表单组件,用了element-ui

此处用了自定义组件的v-model来收集子组件的数据

//InputComponent.vue  要渲染的子组件
<template>
  <el-input :value="username" @input="inputHandler"></el-input>
</template>

<script>
  export default {
    name: 'InputComponent',
    data() {
      return {

      }
    },
    model: {
      prop: 'username',
      event: 'input'
    },
    props: {
      username: String
    },
    methods: {
      inputHandler(ev) {
        console.log(ev)
        this.$emit('input', ev)
      }
    }
  }
</script>

<style scoped>

</style>
//SwitchComponent.vue  要渲染的子组件
<template>
  <el-switch
    :value="checked"
    active-color="#13ce66"
    inactive-color="#ff4949"
    @change="changeHandler"
  >
  </el-switch>
</template>

<script>
  export default {
    name: 'SwitchComponent',
    data() {
      return {

      }
    },
    model: {
      prop: 'checked',
      event: 'change'
    },
    props: {
      checked: {
        default: true
      }
    },
    methods: {
      changeHandler(ev) {
        this.$emit('change', ev)
      }
    }
  }
</script>

<style scoped>

</style>
//SelectComponent.vue  要渲染的子组件
<template>
  <el-select :value="role" placeholder="请选择" @change="changeHandler">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.label">
    </el-option>
  </el-select>
</template>

<script>
  export default {
    name: 'SelectComponent',
    data () {
      return {
        options: [
          {
            value: '1',
            label: '李世民'
          },
          {
            value: '2',
            label: '嬴政'
          },
          {
            value: '3',
            label: '刘邦'
          },
          {
            value: '4',
            label: '项羽'
          },
          {
            value: '5',
            label: '范蠡'
          }
        ],
        value: ''
      }
    },
    model: {
      prop: 'role',
      event: 'change'
    },
    props: {
      role: {
        default: ''
      }
    },
    methods: {
      changeHandler(ev) {
        this.$emit('change', ev)
      }
    }
  }
</script>

<style scoped>

</style>

主组件(父组件)

此处用了自定义组件的v-model来收集子组件的数据

//Main.vue  父组件
<template>
  <div>
      <el-form :model="formData">
        <el-form-item v-for="(item, index) in formItemList" :key="index" :label="item.label">
          <component
            :is="item.type"
            v-model="formData[item.key]"
          >
          </component>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
//引入三个表单组件
  import InputComponent from './subComponents/InputComponent'
  import SelectComponent from './subComponents/SelectComponent'
  import SwitchComponent from './subComponents/SwitchComponent'

  export default {
    name: 'Main',
    data() {
      return {
        //数据的type值要与组件的名字对应  
        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: {

        }
      }
    },
    components: {
      InputComponent,
      SwitchComponent,
      SelectComponent,
    },
    methods: {

    }
  }
</script>

<style scoped lang="less">
  
</style>

vue实现动态表单动态渲染组件的方式(2)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Vue生命周期与setup深入详解

    Vue生命周期与setup深入详解

    Vue的生命周期就是vue实例从创建到销毁的全过程,也就是new Vue() 开始就是vue生命周期的开始。Vue 实例有⼀个完整的⽣命周期,也就是从开始创建、初始化数据、编译模版、挂载Dom -> 渲染、更新 -> 渲染、卸载 等⼀系列过程,称这是Vue的⽣命周期
    2022-09-09
  • vue自定义树状结构图的实现方法

    vue自定义树状结构图的实现方法

    这篇文章主要给大家介绍了关于vue自定义树状结构图的实现方法,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • vue 集成jTopo 处理方法

    vue 集成jTopo 处理方法

    这篇文章主要介绍了vue 集成jTopo 处理方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-08-08
  • 用Vue-cli搭建的项目中引入css报错的原因分析

    用Vue-cli搭建的项目中引入css报错的原因分析

    本篇文章主要介绍了用Vue-cli搭建的项目中引入css报错的原因分析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • vue 使用高德地图vue-amap组件过程解析

    vue 使用高德地图vue-amap组件过程解析

    这篇文章主要介绍了vue 使用高德地图vue-amap组件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • Vue的三种路由模式总结

    Vue的三种路由模式总结

    这篇文章主要介绍了Vue的三种路由模式总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 使用淘宝镜像cnpm安装Vue.js的图文教程

    使用淘宝镜像cnpm安装Vue.js的图文教程

    今天小编就为大家分享一篇使用淘宝镜像cnpm安装Vue.js的图文教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • vue实现div单选多选功能

    vue实现div单选多选功能

    这篇文章主要为大家详细介绍了vue实现div单选多选功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • vue项目实现图形验证码

    vue项目实现图形验证码

    这篇文章主要为大家详细介绍了vue项目实现图形验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • vue前端如何向后端传递参数

    vue前端如何向后端传递参数

    这篇文章主要介绍了vue前端如何向后端传递参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06

最新评论