溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

vue中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化表單

發(fā)布時(shí)間:2021-07-21 14:44:02 來(lái)源:億速云 閱讀:156 作者:Leah 欄目:web開(kāi)發(fā)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)vue中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化表單,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

通過(guò)json配置快速生成表單的vue plugin。

設(shè)計(jì)目標(biāo)

  1. 減少html 重復(fù)片段

  2. 表單字段組件可擴(kuò)展

  3. 事件、聯(lián)動(dòng)通過(guò)eventbus 解耦

  4. 校驗(yàn)可擴(kuò)展

  5. 表單布局可自定義

  6. 可視化配置

大概方案設(shè)計(jì)

vue中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化表單

使用

安裝

npm install charlie-autoform charlie-autoform_component_lib

源碼:https://charlielau.github.io/autoform/#/component/autoform

引入插件

import AutoForm from 'charlie-autoform';
import AutoForm_component_lib from 'charlie-autoform_component_lib';

Vue.use(AutoForm);
Vue.use(AutoForm_component_lib);

基本使用

demo.vue

<template>
 <div>
  <auto-form ref="tagForm1" :model="model1" :fields="fields1" :layout="layout">
   <el-form-item class="clearfix">
   <el-button type="primary">立即創(chuàng)建</el-button>
   <el-button>取消</el-button>
   </el-form-item>
  </auto-form>
 </div>
</template>
<script>
 export default {
 data() {
  return {
  model2: {
   name: '',
   type: []
  },
  layout2: {
   align: 'left',
   labelWidth: '100px',
   custom: false, //是否自定義布局
   inline: true //是否內(nèi)聯(lián)
  },
  fields2: [
   {
   key: 'name',
   type: 'input',
   templateOptions: {
    label: '審批人'
   }
   },
   {
   key: 'region',
   type: 'select',
   templateOptions: {
    label: '活動(dòng)區(qū)域',
    placeholder: '請(qǐng)選擇活動(dòng)區(qū)域',
    options: [
    {
     label: '區(qū)域一',
     value: 'shanghai'
    },
    {
     label: '區(qū)域二',
     value: 'beijing'
    }
    ],
    validators:[ //校驗(yàn)
    // {required:true,message:'必填'}
    // ""
    ]
   }
   }
  ]
  };
 }
 };
</script>

最終效果

vue中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化表單

添加自定義組件或者組件目錄

Vue.$autoform.RegisterDir(()=>require.context('./components/autoform', 'c'));//目錄
Vue.$autoform.Register(Vue,[Components...],{prefix: "c"}) //組件對(duì)象

cHello.vue

// PATH:/components/autoform/cHello.vue
<template>
 <div>
  <div>
   <p>基本的變量可以通過(guò)"mixins"獲取,這里有開(kāi)發(fā)組件需要的一些變量</p>
   <p>自定義子組件:Hello</p>
   <p>當(dāng)前field: {{field}}</p>
   <p>整個(gè)model: {{model}}</p>
   <p>當(dāng)前model: {{model[field.name]}}</p>
   <p>layout: {{layout}}</p>
   <p>字段相關(guān)配置to: {{to}}</p>
  </div>
 </div>
</template>

<script>
 import {baseField} from "charlie-autoform";
 export default {
  mixins: [baseField],
  name: 'cHello',
  data () {
   return {};
  },
  methods: {},
  mounted(){
   //this.eventBus 事件總線(xiàn)
  }
 };
</script>

上述就是小編為大家分享的vue中怎么實(shí)現(xiàn)一個(gè)自動(dòng)化表單了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI