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

Vue2+ElementUI表單、Form組件的封裝過程

 更新時間:2024年03月13日 11:05:16   作者:記億揺晃著的那天  
在 Vue2 項目中,ElementUI 的 el-form 組件是常用的表單組件,它提供了豐富的功能和樣式,可以滿足各種需求,本文給大家介紹Vue2+ElementUI表單、Form組件的封裝過程,感興趣的朋友跟隨小編一起看看吧

Vue2+ElementUI表單、Form組件的封裝 :引言

在 Vue2 項目中,ElementUI 的 el-form 組件是常用的表單組件。它提供了豐富的功能和樣式,可以滿足各種需求。但是,在實際開發(fā)中,我們經(jīng)常會遇到一些重復(fù)性的需求,比如:

  • 需要對表單進行校驗
  • 需要對表單數(shù)據(jù)進行重置
  • 需要在表單中添加額外的功能,比如動態(tài)添加表單項等

為了提高開發(fā)效率,我們可以對 el-form 組件進行封裝,將這些重復(fù)性的需求抽象成通用的功能。這樣,在后續(xù)的項目中,我們就可以直接使用封裝好的組件,而無需重復(fù)開發(fā)。

預(yù)期效果

預(yù)期效果如下。

創(chuàng)建表單組件

先把架子搭起來,組件名為H3yunFormCompV1,這個是隨便取的哈。然后隨便在哪個地方用上,方便測試。

<template>
  <div>
    <el-form>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "H3yunFormCompV1",
  data() {
    return {}
  },
  props: {},
  mounted() {
  },
  methods: {}
}
</script>
<style scoped>
</style>

父組件傳遞表單數(shù)據(jù),子組件遍歷數(shù)據(jù)

formData數(shù)據(jù)傳遞過去。formData是一個列表,每個對象的結(jié)果如下{label: null, value: null}非常的簡單。

 <H3yunFormCompV1 :formData="formData"></H3yunFormCompV1>
data() {
    return {
formData: []
}
}

子組件如下:使用v-for遍歷formData,并把label和value取出來。

<template>
  <div>
    <el-form ref="form" :model="form" :inline="true">
      <el-form-item v-for="(item,key) in formData" :key="key" :label="item.label">
        <el-input v-model="item.value"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "H3yunFormCompV1",
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      }
    }
  },
  props: {
    formData: Array
  },
  mounted() {
  },
  methods: {}
}
</script>
<style scoped>
</style>

添加disabled屬性

子組件的完整代碼

<template>
  <div>
    <el-row>
      <el-form
          ref="form"
          :model="form"
          label-position="top"
          size="small"
      >
        <el-col :span="6" v-for="(item,key) in formData" :key="key">
          <div class="box">
            <el-form-item :label="item.label">
              <el-input v-model="item.value" :disabled="item.disabled"></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
export default {
  name: "H3yunFormCompV1",
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      }
    }
  },
  props: {
    formData: Array
  },
  mounted() {
  },
  methods: {}
}
</script>
<style>
.box {
  font-size: 14px;
  padding: 6px 12px;
  color: #304265;
  background: #f8fafc;
  border-radius: 4px;
  min-height: 22px;
  box-sizing: content-box;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
}
.box .el-form-item__label {
  position: relative;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #304265;
  font-weight: 400 !important;
}
</style>

效果如下:

適配JSON數(shù)據(jù)

先看效果,下面是銷量預(yù)測的json數(shù)據(jù)。

代碼如下:主要是把json數(shù)據(jù)解析為了一個個表單項。

<template>
  <div>
    <el-row>
      <el-form
          ref="form"
          :model="form"
          label-position="top"
          size="small"
      >
        <el-col :span="6" v-for="(item,key) in formStructure" :key="key">
          <div class="box">
            <el-form-item :label="item.label">
              <el-input v-model="item.value" :disabled="item.disabled"></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
export default {
  name: "H3yunFormCompV1",
  data() {
    return {
      form: {},
      formStructure: []
    }
  },
  props: {
    formData: Array
  },
  watch: {
    // 監(jiān)控父組件的表單數(shù)據(jù)
    formData: {
      handler(newFormData) {
        // 當(dāng) formData 變化時執(zhí)行的操作
        // 解析新表單結(jié)構(gòu)
        this.parseFormStructure(newFormData);
      },
      deep: true, // 深度監(jiān)聽,用于監(jiān)聽數(shù)組或?qū)ο髢?nèi)部的變化
    },
  },
  mounted() {
    // 解析新表單結(jié)構(gòu) - 第一次點擊時執(zhí)行
    this.parseFormStructure()
  },
  methods: {
    // 解析表單結(jié)構(gòu)
    parseFormStructure() {
      // 清除表單結(jié)構(gòu)和表單數(shù)據(jù)
      this.formStructure = []
      this.form = {}
      const formStructure = []
      // column的數(shù)據(jù)類型:{ label: null, value: null, prop: null, disabled: null, dataType: null}
      this.formData.forEach(column => {
        if (column.dataType == undefined) {
          column.dataType = 'text'
        }
        // 如果數(shù)據(jù)是json,需要把JSON數(shù)據(jù)裝為column的結(jié)構(gòu)
        if (column.dataType == 'json') {
          const label = column.label
          const prop = column.prop
          const jsonValue = column.value
          const disabled = column.disabled
          const jsonObj = JSON.parse(jsonValue)
          // 構(gòu)建column對象
          Object.keys(jsonObj).forEach(key => {
            const childLabel = `${label}.${key}`
            const childProp = `${prop}.${key}`
            const childValue = jsonObj[key]
            const childDisabled = disabled
            const childColumn = {
              label: childLabel,
              value: childValue,
              prop: childProp,
              disabled: childDisabled,
              dataType: 'text'
            }
            formStructure.push(childColumn)
          })
        } else {
          formStructure.push(column)
        }
      })
      this.formStructure = formStructure
    }
  }
}
</script>
<style>
.box {
  font-size: 14px;
  padding: 6px 12px;
  color: #304265;
  background: #f8fafc;
  border-radius: 4px;
  min-height: 22px;
  box-sizing: content-box;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
}
.box .el-form-item__label {
  position: relative;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #304265;
  font-weight: 400 !important;
}
</style>

表單提交

首先寫個提交按鈕,并添加個change表單提交事件。

 <el-button type="primary" size="small" @change="submitForm">提交</el-button>

提交邏輯如下

// 提交的數(shù)據(jù)在this.form里面,表單驗證通過后可以使用axios把表單數(shù)據(jù)提交到服務(wù)器。
submitForm() {
      // 使用 this.$refs.form.validate() 進行表單驗證
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 表單驗證通過,執(zhí)行提交操作
          // 在這里你可以使用 Axios 或其他方式提交數(shù)據(jù)到后端
          // 示例:假設(shè)有一個名為 submitData 的方法用于提交數(shù)據(jù)
          this.submitData();
        } else {
          // 表單驗證失敗,可以做一些處理,如提示用戶
          this.$message.error('表單驗證失敗,請檢查輸入信息。');
        }
      });
    },
    submitData() {
      // 在這里執(zhí)行提交數(shù)據(jù)的操作
      // 可以使用 Axios 或其他方式發(fā)送數(shù)據(jù)到后端
      // 示例:假設(shè)有一個名為 postData 的方法用于發(fā)送數(shù)據(jù)
      // postData(this.form)
      this.$message.success('表單提交成功!');
    }

到此這篇關(guān)于Vue2+ElementUI表單、Form組件的封裝的文章就介紹到這了,更多相關(guān)Vue2 ElementUI表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實現(xiàn)全選功能

    vue實現(xiàn)全選功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)全選功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • vue-cli3腳手架安裝方法

    vue-cli3腳手架安裝方法

    這篇文章主要介紹了vue-cli3腳手架安裝方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • el-select自定義指令實現(xiàn)觸底加載分頁請求options數(shù)據(jù)(完整代碼和接口可直接用)

    el-select自定義指令實現(xiàn)觸底加載分頁請求options數(shù)據(jù)(完整代碼和接口可直接用)

    某些情況下,下拉框需要做觸底加載,發(fā)請求,獲取option的數(shù)據(jù),下面給大家分享el-select自定義指令實現(xiàn)觸底加載分頁請求options數(shù)據(jù)(附上完整代碼和接口可直接用),感興趣的朋友參考下吧
    2024-02-02
  • Vue使用extend動態(tài)創(chuàng)建組件的實現(xiàn)

    Vue使用extend動態(tài)創(chuàng)建組件的實現(xiàn)

    本文主要介紹了Vue使用extend動態(tài)創(chuàng)建組件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • vue中ref的用法小結(jié)

    vue中ref的用法小結(jié)

    在項目中使用ref時有時候直接取值,有時候返回的卻是一個數(shù)組,不知其中緣由,后查了一下ref用法,在這里給大家分享vue中ref的用法,感興趣的朋友一起看看吧
    2023-11-11
  • vue項目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案

    vue項目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案

    這篇文章主要介紹了vue項目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue vxe-table使用問題收錄小結(jié)

    Vue vxe-table使用問題收錄小結(jié)

    這篇文章主要為大家介紹了Vue vxe-table使用問題收錄小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 分享一個vue項目“腳手架”項目的實現(xiàn)步驟

    分享一個vue項目“腳手架”項目的實現(xiàn)步驟

    這篇文章主要介紹了分享一個vue項目“腳手架”項目的實現(xiàn)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Vue實例中el和data的兩種寫法小結(jié)

    Vue實例中el和data的兩種寫法小結(jié)

    這篇文章主要介紹了Vue實例中el和data的兩種寫法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-11-11
  • vuecli+AXdownload下載組件封裝?+css3下載懸浮球動畫效果

    vuecli+AXdownload下載組件封裝?+css3下載懸浮球動畫效果

    當(dāng)觸發(fā)下載功能的時候,會觸發(fā)一個下載動畫,下載懸浮球會自動彈出,并且閃爍提示有新的下載任務(wù)直到下載任務(wù)完場提示,接下來通過本文介紹vuecli+AXdownload下載組件封裝?+css3下載懸浮球動畫效果,需要的朋友可以參考下
    2024-05-05

最新評論