最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)常會遇到一些重復性的需求,比如:

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

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

預期效果

預期效果如下。

創(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ù)

先看效果,下面是銷量預測的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) {
        // 當 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)文章

最新評論

崇文区| 镇坪县| 宁城县| 旅游| 乡城县| 松原市| 连州市| 柯坪县| 张家口市| 油尖旺区| 海盐县| 巴林右旗| 东乌珠穆沁旗| 浪卡子县| 星座| 陆川县| 齐河县| 阜新| 拉萨市| 开江县| 恩平市| 建宁县| 修水县| 田阳县| 浦城县| 满城县| 潼关县| 南涧| 镇康县| 绥宁县| 五大连池市| 芜湖县| 梧州市| 高邑县| 惠水县| 平邑县| 青州市| 武功县| 开封市| 手游| 池州市|