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

vue 添加和編輯用同一個(gè)表單,el-form表單提交后清空表單數(shù)據(jù)操作

 更新時(shí)間:2020年08月03日 15:11:13   作者:隨意花  
這篇文章主要介紹了vue 添加和編輯用同一個(gè)表單,el-form表單提交后清空表單數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在項(xiàng)目中做聯(lián)系人的添加和編輯功能,點(diǎn)擊父級(jí)頁(yè)面的添加和編輯按鈕,用的是同一個(gè)表單彈窗,數(shù)據(jù)添加和編輯用同一個(gè)彈窗,沒(méi)有在彈窗使用v-if,性能不是很好,彈窗中有表單,在編輯彈窗表單數(shù)據(jù)之后關(guān)閉彈窗,然后點(diǎn)擊添加的時(shí)候,彈窗里的表單數(shù)據(jù)還是之前編輯的數(shù)據(jù),無(wú)法做到清空表單數(shù)據(jù),接下來(lái)是解決方法了,嘿嘿

首先是不管是添加還是編輯,都需要將子組件需要的對(duì)象屬性一一寫(xiě)出來(lái),傳給子組件,

然后是主要用到了el-form表單有一個(gè)清空重置表單數(shù)據(jù)的事件方法resetField(),在子組件表單彈窗打開(kāi)的時(shí)候清空一下,在關(guān)閉子組件表單彈窗的時(shí)候還需要調(diào)用resetField()去重置表單數(shù)據(jù)。這樣編輯數(shù)據(jù)之后再次打開(kāi)添加數(shù)據(jù),頁(yè)面不會(huì)有之前的數(shù)據(jù)存在,也不會(huì)出現(xiàn)驗(yàn)證信息在頁(yè)面上。

1. 在父級(jí)頁(yè)面調(diào)用子級(jí)彈框表單組件(AddEdit.vue)

 <!-- form是子組件的form表單數(shù)據(jù),meg是子組件彈窗的標(biāo)題(添加或者編輯) -->
 <!-- <add-edit :msg.sync="msg" v-if='msg' :form='form'></add-edit> -->
 <!-- 沒(méi)有使用v-if 是因?yàn)轭l繁點(diǎn)擊編輯和新增的話,性能方面不是很好-->
<template>
 <el-button @click='addClick'>添加</el-button>
 <el-button @click='editClick(scope.row)'>編輯</el-button>
 <!-- 子組件彈窗 -->
 <add-edit :msg.sync="msg" :form='formData'></add-edit>
</template>
<script>
export default {
 data() {
 return {
  formData: {}
 }
 },
 
 methods: {
 addClick() {
 //需要將子組件需要的對(duì)象屬性傳過(guò)去,這一步必須得有,這樣在子組件才可以清空表單
  this.formData = {
  name: '',
  email: '',
  phone: ''
  }
  this.msg = '添加'
 },
 
 editClick(row) {
  this.formData = row;
  this.msg = '編輯'
 }
 }
}
</script>

2. 點(diǎn)擊父級(jí)頁(yè)面的編輯按鈕,將人員信息傳遞給AddEdit.vue

<template>
 <el-dialog :visible.sync="isShow" width="500px" class="edit-contact" :before-close="closeDialog">
  <span slot="title">{{msg}}聯(lián)系人</span>
  <el-form :model="form" ref="ruleForm" label-width="100px" :rules="rules" size="small">
   <el-form-item :label="it.label" :prop="it.prop" v-for="it in formLabel" :key="it.prop">
    <el-input v-model="form[it.prop]" :placeholder="`請(qǐng)輸入${it.label}`"></el-input>
   </el-form-item>
  </el-form>
  <div class="base-btn-action">
   <el-button size="small" type="primary" @click="saveContact">{{form.id?'編輯':'添加'}}</el-button>
   <el-button size="small" @click="closeDialog">取 消</el-button>
  </div>
 </el-dialog>
</template>
<script>
export default {
 props: {
  msg: {
   //“添加”或者“編輯”
   type: String,
   default: ""
  },
  form: {
  //接收父組件傳過(guò)來(lái)得對(duì)象數(shù)據(jù)
   type: Object,
   default: () => {}
  }
 },
 data() {
  return {
   formLabel: [
    { label: "姓名", prop: "name" },
    { label: "郵箱", prop: "email" },
    { label: "聯(lián)系方式", prop: "phone" }
   ],
   rules: {
    name: [{ required: true, message: "請(qǐng)輸入姓名", trigger: "change" }],
    email: [
     { required: true, message: "請(qǐng)輸入郵箱", trigger: "change" },
     { type: "email", message: "請(qǐng)輸入正確的郵箱地址", trigger: ["blur"] }
    ],
    phone: [
     { required: true, message: "請(qǐng)輸入手機(jī)號(hào)", trigger: "change" }
    ]
   }
  };
 },
 computed: {
 //通過(guò)props的數(shù)據(jù)msg的值是否為空來(lái)判斷彈框顯示與否
  isShow() {
   return this.msg === "" ? false : true;
  }
 },
 watch: {
 //監(jiān)聽(tīng)子組件彈窗是否打開(kāi)
  msg(n) {
  //子組件打開(kāi)得情況
    if (n !== '') {
     if (!this.$refs.ruleForm) {
     //初次打開(kāi)子組件彈窗的時(shí)候,form表單dom元素還沒(méi)加載成功,需要異步獲取
      this.$nextTick(() => {
       this.$refs.ruleForm.resetFields() // 去除驗(yàn)證
      })
     } else {
     //再次打開(kāi)子組件彈窗,子組件彈窗的form表單dom元素已經(jīng)加載好了,不需要異步獲取
      this.$refs.ruleForm.resetFields() // 去除驗(yàn)證
     }
    }
   },
 },
 methods: {
  closeDialog() {
   this.$emit("update:msg", "");
   setTimeout(() => {
   //關(guān)閉彈窗的時(shí)候表單也重置為初始值并移除校驗(yàn)結(jié)果
    this.$refs.ruleForm.resetFields();
   }, 200);
  }
 }
};
</script>

好了,問(wèn)題解決了,在此記錄一下,以后可以翻回來(lái)再看看!

以上這篇vue 添加和編輯用同一個(gè)表單,el-form表單提交后清空表單數(shù)據(jù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論