欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue element封裝form表單的實現(xiàn)

 更新時間:2023年06月15日 10:13:56   作者:山水有輕音  
本文主要介紹了vue element封裝form表單的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、新建FormComponent.vue

1.梳理思路,form表單有各種類型,常見的有input、select,radio,checkbox,time,date,date-picker,textarea,rate等

<template>
? <el-form ref="form" :model="formData" label-width="80px">
? ? <el-form-item
? ? ? v-for="item in formList"
? ? ? :key="item.prop"
? ? ? :label="item.label"
? ? ? :prop="item.prop"
? ? >
? ? ? <div class="form-item-container">
? ? ? ? <template v-if="item.prefix">
? ? ? ? ? <span class="prefix">{{ item.prefix }}</span> ?--前置單位
? ? ? ? </template>
? ? ? ? <template v-if="item.type === 'input'">
? ? ? ? ? <el-input
? ? ? ? ? ? v-model="formData[item.prop]"
? ? ? ? ? ? :placeholder="item.placeholder"
? ? ? ? ? ? :clearable="item.clearable"
? ? ? ? ? ? :maxlength="item.maxlength"
? ? ? ? ? ? @change="handleChange(item.prop, formData[item.prop])"
? ? ? ? ? />
? ? ? ? </template>
? ? ? ? <template v-if="item.type === 'textarea'">
? ? ? ? ? <el-input
? ? ? ? ? ? type="textarea"
? ? ? ? ? ? v-model="formData[item.prop]"
? ? ? ? ? ? :placeholder="item.placeholder"
? ? ? ? ? ? :clearable="item.clearable"
? ? ? ? ? ? :maxlength="item.maxlength"
? ? ? ? ? ? @change="handleChange(item.prop, formData[item.prop])"
? ? ? ? ? />
? ? ? ? </template>
? ? ? ? <template v-if="item.type === 'select'">
? ? ? ? ? <el-select
? ? ? ? ? ? v-model="formData[item.prop]"
? ? ? ? ? ? :placeholder="item.placeholder"
? ? ? ? ? ? :clearable="item.clearable"
? ? ? ? ? ? @change="handleChange(item.prop, formData[item.prop])"
? ? ? ? ? >
? ? ? ? ? ? <el-option
? ? ? ? ? ? ? v-for="option in item.options"
? ? ? ? ? ? ? :key="option.value"
? ? ? ? ? ? ? :label="option.label"
? ? ? ? ? ? ? :value="option.value"
? ? ? ? ? ? />
? ? ? ? ? </el-select>
? ? ? ? </template>
? ? ? ? <!-- 其它表單項同理 -->
? ? ? ? <template v-if="item.suffix">
? ? ? ? ? <span class="suffix">{{ item.suffix }}</span> ?--后置
? ? ? ? </template>
? ? ? </div>
? ? ? <div class="form-item-unit" v-if="item.unit">{{ item.unit }}</div> --單位
? ? </el-form-item>
? ? <div class="form-body" v-if="!$slots.default">
? ? ? <slot></slot>
? ? </div>
? ? <div class="form-footer" v-if="$slots.footer">
? ? ? <slot name="footer"></slot>
? ? </div>
? ? <div class="form-footer" v-else>
? ? ? <el-form-item>
? ? ? ? <el-button type="primary" @click="onSubmit" v-if="showSubmitButton">提交</el-button>
? ? ? ? <el-button @click="onCancel" v-if="showCancelButton">取消</el-button>
? ? ? </el-form-item>
? ? </div>
? </el-form>
</template>
<script>
export default {
? props: {
? ? formList: {
? ? ? type: Array,
? ? ? required: true,
? ? },
? ? formData: {
? ? ? type: Object,
? ? ? default: () => ({}),
? ? },
? ? actionUrl: {
? ? ? type: String,
? ? ? required: true,
? ? },
? ? showSubmitButton: {
? ? ? type: Boolean,
? ? ? default: true,
? ? },
? ? showCancelButton: {
? ? ? type: Boolean,
? ? ? default: true,
? ? },
? },
? methods: {
? ? handleChange(prop, value) {
? ? ? this.$emit('update:formData', {
? ? ? ? ...this.formData,
? ? ? ? [prop]: value,
? ? ? });
? ? },
? ? onSubmit() {
? ? ? this.$refs.form.validate((valid) => {
? ? ? ? if (valid) {
? ? ? ? ? this.$axios.post(this.actionUrl, this.formData).then((res) => {
? ? ? ? ? ? this.$emit('submit-success', res);//回調(diào)
? ? ? ? ? });
? ? ? ? } else {
? ? ? ? ? this.$message.error('請檢查表單是否填寫正確');
? ? ? ? }
? ? ? });
? ? },
? ? onCancel() {
? ? ? this.$emit('cancel');
? ? },
? },
};
</script>
<style>
.form-item-container {
? position: relative;
? display: inline-block;
}
.prefix,
.suffix {
? position: absolute;
? top: 50%;
? transform: translateY(-50%);
? font-size: 14px;
}
.prefix {
? left: 8px;
}
.suffix {
? right: 8px;
}
.form-item-unit {
? display: inline-block;
? margin-left: 8px;
? color: #909399;
}
.form-body {
? margin-bottom: 20px;
? border: 1px solid #e4e7ed;
? border-radius: 4px;
? padding: 20px;
}
.form-footer {
? text-align: right;
? margin-top: 20px;
}
</style>

二、如何使用

引入import formComponent from 'xx';

<template>
? <div>
? ? <form-component
? ? ? :form-list="formList"
? ? ? :form-data="formData"
? ? ? :action-url="actionUrl"
? ? ? :show-submit-button="true"
? ? ? :show-cancel-button="true"
? ? ? @submit-success="onSubmitSuccess"
? ? ? @cancel="onCustomCancel"
? ? ? >
? ? ? <!-- 自定義表單內(nèi)容 -->
? ? ? <template #default>
? ? ? ? <el-form-item>
? ? ? ? ? <el-radio-group v-model="formData.gender">
? ? ? ? ? ? <el-radio label="male">男性</el-radio>
? ? ? ? ? ? <el-radio label="female">女性</el-radio>
? ? ? ? ? </el-radio-group>
? ? ? ? </el-form-item>
? ? ? ? <el-form-item>
? ? ? ? ? <el-cascader
? ? ? ? ? ? v-model="formData.address"
? ? ? ? ? ? :options="addressOptions"
? ? ? ? ? ? placeholder="請選擇地址"
? ? ? ? ? ? @change="handleChange('address', formData['address'])"
? ? ? ? ? />
? ? ? ? </el-form-item>
? ? ? ? <el-form-item>
? ? ? ? ? <el-input
? ? ? ? ? ? v-model="formData.email"
? ? ? ? ? ? placeholder="請輸入電子郵箱"
? ? ? ? ? ? clearable
? ? ? ? ? ? @change="handleChange('email', formData['email'])"
? ? ? ? ? />
? ? ? ? </el-form-item>
? ? ? </template>
? ? ? <!-- 自定義底部按鈕,也可以不添加,使用默認底部按鈕 -->
? ? ? <template #footer>
? ? ? ? <el-button type="primary" @click="onCustomSubmit">提交</el-button>
? ? ? ? <el-button @click="onCustomCancel">取消</el-button>
? ? ? </template>
? ? </form-component>
? </div>
</template>
<script>
import FormComponent from './FormComponent.vue';
export default {
? components: {
? ? FormComponent,
? },
? data() {
? ? return {
? ? ? formList: [
? ? ? ? {
? ? ? ? ? label: '姓名',
? ? ? ? ? prop: 'name',
? ? ? ? ? type: 'input',
? ? ? ? ? placeholder: '請輸入姓名',
? ? ? ? ? clearable: true,
? ? ? ? ? maxlength: 20,
? ? ? ? },
? ? ? ? {
? ? ? ? ? label: '性別',
? ? ? ? ? prop: 'gender',
? ? ? ? ? type: 'select',
? ? ? ? ? placeholder: '請選擇性別',
? ? ? ? ? clearable: true,
? ? ? ? ? options: [
? ? ? ? ? ? {
? ? ? ? ? ? ? label: '男性',
? ? ? ? ? ? ? value: 'male',
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? label: '女性',
? ? ? ? ? ? ? value: 'female',
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? },
? ? ? ? {
? ? ? ? ? label: '地址',
? ? ? ? ? prop: 'address',
? ? ? ? ? type: 'cascader',
? ? ? ? ? placeholder: '請選擇地址',
? ? ? ? ? clearable: true,
? ? ? ? ? options: [],
? ? ? ? },
? ? ? ? {
? ? ? ? ? label: '電子郵箱',
? ? ? ? ? prop: 'email',
? ? ? ? ? type: 'input',
? ? ? ? ? placeholder: '請輸入電子郵箱',
? ? ? ? ? clearable: true,
? ? ? ? ? maxlength: 50,
? ? ? ? },
? ? ? ],
? ? ? formData: {
? ? ? ? name: '',
? ? ? ? gender: '',
? ? ? ? address: [],
? ? ? ? email: '',
? ? ? },
? ? ? addressOptions: [
? ? ? ? {
? ? ? ? ? value: 'beijing',
? ? ? ? ? label: '北京',
? ? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'haidian',
? ? ? ? ? ? ? label: '海淀區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'zizhuqiao',
? ? ? ? ? ? ? ? ? label: '紫竹橋',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'xizhimen',
? ? ? ? ? ? ? ? ? label: '西直門',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'chaoyang',
? ? ? ? ? ? ? label: '朝陽區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'guomao',
? ? ? ? ? ? ? ? ? label: '國貿(mào)',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'cbd',
? ? ? ? ? ? ? ? ? label: 'CBD',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? },
? ? ? ? {
? ? ? ? ? value: 'shanghai',
? ? ? ? ? label: '上海',
? ? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'xuhui',
? ? ? ? ? ? ? label: '徐匯區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'xujiahui',
? ? ? ? ? ? ? ? ? label: '徐家匯',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'pudong',
? ? ? ? ? ? ? label: '浦東新區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'lujiazui',
? ? ? ? ? ? ? ? ? label: '陸家嘴',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? },
? ? ? ],
? ? ? actionUrl: '/api/submit-form',
? ? };
? },
? methods: {
? ? onCustomSubmit() {
? ? ? // 自定義提交按鈕的點擊事件
? ? },
? ? onCustomCancel() {
? ? ? // 自定義取消按鈕的點擊事件
? ? },
? ? onSubmitSuccess(res) {
? ? ? // 表單提交成功的回調(diào)函數(shù)
? ? },
? },
};
</script>

到此這篇關于vue element封裝form表單的實現(xiàn)的文章就介紹到這了,更多相關vue element封裝form表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Vue集成阿里云做滑塊驗證的實踐

    Vue集成阿里云做滑塊驗證的實踐

    滑塊驗證是比較常見的人機鑒別的方法,本文主要介紹了Vue集成阿里云做滑塊驗證,具有一定的參考價值,感興趣的可以了解一下
    2022-01-01
  • Vue無限滑動周選擇日期的組件的示例代碼

    Vue無限滑動周選擇日期的組件的示例代碼

    這篇文章主要介紹了Vue無限滑動周選擇日期的組件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 詳解vantUI框架在vue項目中的應用踩坑

    詳解vantUI框架在vue項目中的應用踩坑

    這篇文章主要介紹了詳解vantUI框架在vue項目中的應用踩坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • VUE開發(fā)分布式醫(yī)療掛號系統(tǒng)后臺管理頁面步驟

    VUE開發(fā)分布式醫(yī)療掛號系統(tǒng)后臺管理頁面步驟

    本文從整體上介紹Vue框架的開發(fā)流程,結合具體的案例,使用Vue框架調(diào)用具體的后端接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • vue項目中vue.config.js文件詳解

    vue項目中vue.config.js文件詳解

    vue.config.js?是一個可選的配置文件,如果項目的?(和?package.json?同級的)?根目錄中存在這個文件,那么它會被?@vue/cli-service?自動加載,這篇文章主要介紹了vue項目中vue.config.js文件的介紹,需要的朋友可以參考下
    2024-02-02
  • Vue開發(fā)Sort組件代碼詳解

    Vue開發(fā)Sort組件代碼詳解

    這篇文章主要介紹了Vue開發(fā)Sort組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • vue+iview實現(xiàn)分頁及查詢功能

    vue+iview實現(xiàn)分頁及查詢功能

    這篇文章主要為大家詳細介紹了vue+iview實現(xiàn)分頁及查詢功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • element-ui直接在表格中點擊單元格編輯

    element-ui直接在表格中點擊單元格編輯

    最近通過在網(wǎng)上查找資料,自己整合研究了一個可以直接在表格中操作數(shù)據(jù)的基于element-ui的前端模板。可以讓增改數(shù)據(jù)的操作顯得方便一點,感興趣的可以了解一下
    2021-12-12
  • 如何在vue3+ts項目中使用query和params傳參

    如何在vue3+ts項目中使用query和params傳參

    Vue3中的路由傳參有兩種方式:query和params,下面這篇文章主要給大家介紹了關于如何在vue3+ts項目中使用query和params傳參的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • Vue實現(xiàn)多個關鍵詞高亮顯示功能

    Vue實現(xiàn)多個關鍵詞高亮顯示功能

    在現(xiàn)代網(wǎng)頁開發(fā)中,常常需要實現(xiàn)高亮顯示關鍵詞的功能,這篇文章將探討如何通過?Vue.js?中的?highlightText來實現(xiàn)這一功能,感興趣的可以了解下
    2024-12-12

最新評論