form-create的:rule通过JSON动态生成,自定义组件实现复用

form-create的:rule通过JSON动态生成,自定义组件实现复用

npm安装
npm i @form-create/element-ui
main.js引入
import formCreate, {maker} from ‘@form-create/element-ui’
Vue.use(formCreate)

<form-create :rule="formRule" :model="formData" ref="formData" ></form-create>

首先再data定义rule存储需要用到的组件库,formRule:[]为form-create动态创建表单规则

rule: [{
        type: 'h-title',
        title: '', // label名称
        field: '', // 字段名称
        value: '', // input值,
        props: {
          text: "",
          primaryIndex: null,
          id: ""
        }
      },
      {
        type: 'input',
        title: '输入框', // label名称
        field: '', // 字段名称
        value: '', // input值,
        col: {
          span: 12
        }]

mounted中实现自定义组件

Vue.component('h-title', {
      template: `<h3 :key="primaryIndex" :id="id">{{text}}</h3>`,
      props: {
        text: {
          type: String,
          required: true
        },
        primaryIndex: {
          type: Number,
          required: true
        },
        id: {
          type: String,
          required: true
        }
      }
    })

js中实现formRule的动态组装

		  this.formRule = []; // 初始化
          this.addForm.formArray.forEach((item, index) => {
            let title = {};
            title = JSON.parse(JSON.stringify(this.rule[0]))  // **一定要使用深拷贝**
            title.props.primaryIndex = this.steps[index].index;
            title.props.id = this.steps[index].id;
            title.props.text = this.steps[index].title;
            console.log('title====', title)
            this.formRule.push(title);
          });

自定义组件复用效果
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值