使用背景
- 集團(tuán)需求,希望可以動態(tài)設(shè)計表單影兽,收集所需信息蹂空,報表填寫,定時上交
- 那如果我手動寫表單第焰,哪還有摸魚時間
技術(shù)棧
vue3 + elementUI-plus
展示
安裝使用
方式一
npm i vform3-builds
方式二
-
運行官方源碼买优,打包 lib,會在 dist 文件中分別生成四個文件
designer.style.css designer.umd.js render.style.css render.umd.js
引入到項目放在
/lib/vform
中 main.js 中引入上述四個文件挺举,使用方式參考下方文檔
注意:VForm 3依賴Element Plus杀赢,需要先安裝Element Plus
使用 VFormDesigner 組件
它是表單設(shè)計器,就是拖拽設(shè)計的部分湘纵,設(shè)計完成脂崔,生成特定的 json 數(shù)據(jù)結(jié)構(gòu),頁面才可以用另一個組件加載出來梧喷,體驗一下就知道了
在 main.js 中引入
import ElementPlus from 'element-plus' //引入element-plus庫
import VForm3 from 'vform3-builds' //引入VForm3庫
import 'element-plus/dist/index.css' //引入element-plus樣式
import 'vform3-builds/dist/designer.style.css' //引入VForm3樣式
const app = createApp(App)
app.use(ElementPlus) //全局注冊element-plus
app.use(VForm3) //全局注冊VForm3(同時注冊了v-form-designe砌左、v-form-render等組件)
/* 注意:如果你的項目中有使用axios脖咐,請用下面一行代碼將全局axios復(fù)位為你的axios!汇歹! */
window.axios = axios
app.mount('#app')
在 vue 模板中使用
<template>
<v-form-designer></v-form-designer>
</template>
<script setup>
</script>
<style lang="scss">
body {
margin: 0; /* 如果頁面出現(xiàn)垂直滾動條屁擅,則加入此行CSS以消除之 */
}
</style>
使用 VFormRender 組件
它是表單渲染的,利用上面設(shè)計器組件生成的 json 數(shù)據(jù)結(jié)構(gòu)秤朗,渲染出表單內(nèi)容
重要提示:如果你在項目中已按照上一節(jié)文檔注冊了VFormDesigner組件煤蹭,則不再需要注冊VFormRender組件。僅當(dāng)在項目中獨立使用VFormRender組件(即項目中不含表單設(shè)計器)時才需要注冊取视。
在 main.js 中引入
import { createApp } from 'vue'
import App from './App.vue'
import axios from 'axios' //如果需要axios硝皂,請引入
import ElementPlus from 'element-plus' //引入element-plus庫
import VFormRender from 'vform3-builds/dist/render.umd.js' //引入VFormRender組件
import 'element-plus/dist/index.css' //引入element-plus樣式
import 'vform3-builds/dist/render.style.css' //引入VFormRender樣式
const app = createApp(App)
app.use(ElementPlus) //全局注冊element-plus
app.use(VFormRender) //全局注冊VFormRender等組件
/* 注意:如果你的項目中有使用axios,請用下面一行代碼將全局axios復(fù)位為你的axiosW魈贰稽物! */
window.axios = axios
app.mount('#app')
在 vue 模板中使用
<template>
<div>
<v-form-render :form-json="formJson" :form-data="formData" :option-data="optionData" ref="vFormRef">
</v-form-render>
<el-button type="primary" @click="submitForm">Submit</el-button>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
/* 注意:formJson是指表單設(shè)計器導(dǎo)出的json,此處演示的formJson只是一個空白表單jsonU矍贰贝或! */
const formJson = reactive({"widgetList":[],"formConfig":{"modelName":"formData","refName":"vForm","rulesName":"rules","labelWidth":80,"labelPosition":"left","size":"","labelAlign":"label-left-align","cssCode":"","customClass":"","functions":"","layoutType":"PC","jsonVersion":3,"onFormCreated":"","onFormMounted":"","onFormDataChange":"","onFormValidate":""}})
const formData = reactive({})
const optionData = reactive({})
const vFormRef = ref(null)
// 顯示彈框
function show(row) {
showDialog.value = true
reportName.value = row.reportName
http('detail', { formDataId: row.formDataId }).then((res) => {
// 根據(jù) JSON 結(jié)構(gòu),渲染出頁面表單
vFormRef.value.setFormJson(res.formConfigDto)
// 根據(jù)數(shù)據(jù)渲染出頁面填入的值锐秦,填過
nextTick(() => {
vFormRef.value.setFormData(res.formDataDto)
nextTick(() => {
// 填過的數(shù)據(jù)展示給人的時候咪奖,禁用表單,不能更改
vFormRef.value.disableForm()
})
})
})
}
</script>
官方
https://www.yuque.com/visualdev/vform3/using-render
如有不懂可以下方留言酱床,或者查看官方文檔
高級例子后面用到會繼續(xù)補(bǔ)寫文檔