vue自動化表單實例分析
更新時間:2018年05月06日 15:09:16 作者:CharlieLau
本篇文章通過實例給大家分享了vue自動化表單的操作方法以及相關的代碼做了描述,有興趣的朋友可以跟著學習下。
背景
B端系統(tǒng)表單較多,且表單可能含有較多字段
字段較多的表單帶來了大片HTML代碼
在大片HTML中,混雜著參數(shù)綁定、事件處理等邏輯,不利于維護
技術棧 Vue,Element(默認表單布局)適合中后臺項目快速開發(fā)
目標
通過json配置快速生成表單的vue plugin。
設計目標
- 減少html 重復片段
- 表單字段組件可擴展
- 事件、聯(lián)動通過eventbus 解耦
- 校驗可擴展
- 表單布局可自定義
- 可視化配置
大概方案設計
使用
安裝
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 //是否內聯(lián) }, fields2: [ { key: 'name', type: 'input', templateOptions: { label: '審批人' } }, { key: 'region', type: 'select', templateOptions: { label: '活動區(qū)域', placeholder: '請選擇活動區(qū)域', options: [ { label: '區(qū)域一', value: 'shanghai' }, { label: '區(qū)域二', value: 'beijing' } ], validators:[ //校驗 // {required:true,message:'必填'} // "" ] } } ] }; } }; </script>
最終效果
添加自定義組件或者組件目錄
Vue.$autoform.RegisterDir(()=>require.context('./components/autoform', 'c'));//目錄 Vue.$autoform.Register(Vue,[Components...],{prefix: "c"}) //組件對象
cHello.vue
// PATH:/components/autoform/cHello.vue <template> <div> <div> <p>基本的變量可以通過"mixins"獲取,這里有開發(fā)組件需要的一些變量</p> <p>自定義子組件:Hello</p> <p>當前field: {{field}}</p> <p>整個model: {{model}}</p> <p>當前model: {{model[field.name]}}</p> <p>layout: {{layout}}</p> <p>字段相關配置to: {{to}}</p> </div> </div> </template> <script> import {baseField} from "charlie-autoform"; export default { mixins: [baseField], name: 'cHello', data () { return {}; }, methods: {}, mounted(){ //this.eventBus 事件總線 } }; </script>
成果
目前應用再多個系統(tǒng)
定性: 維護成本降低、關注點分離
定量:表單開發(fā)效率提升50%
相關文章
vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法
vue3 集成 echarts 最大的坑就是出現(xiàn)了,reactive 的數(shù)據(jù) 刷新了,但圖表缺不會刷新,所以本文就給大家詳細的介紹一下vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法,需要的朋友可以參考下2023-08-08