el-form組件使用resetFields重置失效的問(wèn)題解決
el-form組件使用resetFields重置失效問(wèn)題解決,有效避坑
1. 問(wèn)題
用el-form寫(xiě)了包含三個(gè)字段的表單,使用resetFields方法進(jìn)行重置,發(fā)現(xiàn)點(diǎn)擊重置或要清空校驗(yàn)時(shí)是失效的。
代碼:
<template> <div> <el-form :inline="true" size="mini" style="float: right" :model="queryParams.args" ref="queryForm"> <el-form-item label="姓名"> <el-input style="width:160px" size="mini" placeholder="" v-model.trim="queryParams.args.name" clearable ></el-input> </el-form-item> <el-form-item label="賬號(hào)"> <el-input style="width:160px" size="mini" placeholder="" v-model.trim="queryParams.args.no" clearable ></el-input> </el-form-item> <el-form-item label="性別"> <el-select v-model="queryParams.args.sex" placeholder="請(qǐng)選擇" clearable> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> </el-form-item> <el-form-item> <el-button type="primary" @click.native="search" size="mini">查詢(xún)</el-button> <el-button type="primary" @click.native="addUseroperator" size="mini">新增</el-button> <el-button type="success" @click.native="reset" size="mini">重置</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { queryParams: { args:{ name: "", no: "", sex: null } }, options: [ {value: 0, label: '女'}, {value: 1, label: '男'} ] } }, methods: { reset(){ this.$refs["queryForm"].resetFields() }, } } </script>
點(diǎn)擊重置按鈕后,仍不能清空重置該表單:
剛開(kāi)始以為是異步問(wèn)題,在重置方法代碼上加上方法this.$nextTick(()=>{}):
methods: { reset(){ this.$nextTick(()=>{ this.$refs["queryForm"].resetFields() }) }, }
測(cè)試后仍然沒(méi)有效果,只能下一步解決:
2. 解決
仔細(xì)檢查代碼后,可以發(fā)現(xiàn)el-form-item沒(méi)有添加prop屬性,導(dǎo)致識(shí)別不到相應(yīng)的字段屬性,注意這里有個(gè)坑:就是el-form這里要加上“:model”字段,我這里是加“:model=“queryParams.args”,如果不加,resetFields也會(huì)同樣失效。加上后的代碼如下:
<template> <div> <el-form :inline="true" size="mini" style="float: right" :model="queryParams.args" ref="queryForm"> <el-form-item label="姓名" prop="name"> <el-input style="width:160px" size="mini" placeholder="" v-model.trim="queryParams.args.name" clearable ></el-input> </el-form-item> <el-form-item label="賬號(hào)" prop="no"> <el-input style="width:160px" size="mini" placeholder="" v-model.trim="queryParams.args.no" clearable ></el-input> </el-form-item> <el-form-item label="性別" prop="sex"> <el-select v-model="queryParams.args.sex" placeholder="請(qǐng)選擇" clearable> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> </el-form-item> <el-form-item> <el-button type="primary" @click.native="search" size="mini">查詢(xún)</el-button> <el-button type="primary" @click.native="addUseroperator" size="mini">新增</el-button> <el-button type="success" @click.native="reset" size="mini">重置</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { queryParams: { args:{ name: "", no: "", sex: null } }, options: [ {value: 0, label: '女'}, {value: 1, label: '男'} ] } }, methods: { reset(){ this.$refs["queryForm"].resetFields() }, } } </script>
點(diǎn)擊重置按鈕測(cè)試后,確實(shí)有效了,問(wèn)題解決。
以上就是el-form組件使用resetFields重置失效的問(wèn)題解決的詳細(xì)內(nèi)容,更多關(guān)于el-form使用resetFields重置失效的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue源碼中要const _toStr = Object.prototype.toString的原因分析
這篇文章主要介紹了Vue源碼中要const _toStr = Object.prototype.toString的原因分析,在文中給大家提到了Object.prototype.toString方法的原理,需要的朋友可以參考下2018-12-12Vue echarts畫(huà)甘特圖流程詳細(xì)講解
這篇文章主要介紹了Vue echarts畫(huà)甘特圖流程,甘特圖(Gantt chart)又稱(chēng)為橫道圖、條狀圖(Bar chart)。其通過(guò)條狀圖來(lái)顯示項(xiàng)目、進(jìn)度和其他時(shí)間相關(guān)的系統(tǒng)進(jìn)展的內(nèi)在關(guān)系隨著時(shí)間進(jìn)展的情況2022-09-09vue前端通過(guò)騰訊接口獲取用戶(hù)ip的全過(guò)程
今天在寫(xiě)項(xiàng)目掉接口的時(shí)候有一個(gè)接口需要到了用戶(hù)的ip地址,查了半天覺(jué)得這個(gè)方法不錯(cuò),下面這篇文章主要給大家介紹了關(guān)于vue前端通過(guò)騰訊接口獲取用戶(hù)ip的相關(guān)資料,需要的朋友可以參考下2022-12-12vue調(diào)試工具vue-devtools的安裝全過(guò)程
這篇文章主要介紹了vue調(diào)試工具vue-devtools的安裝全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue2實(shí)現(xiàn)無(wú)感刷新token的方式詳解
在Web應(yīng)用中,用戶(hù)需要通過(guò)認(rèn)證和授權(quán)才能訪(fǎng)問(wèn)受保護(hù)的資源,為了實(shí)現(xiàn)認(rèn)證和授權(quán)功能,通常需要使用Token來(lái)標(biāo)識(shí)用戶(hù)身份并驗(yàn)證其權(quán)限,本文給大家介紹了vue2實(shí)現(xiàn)無(wú)感刷新token的方式,需要的朋友可以參考下2024-02-02Vue的路由及路由鉤子函數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Vue的路由及路由鉤子函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Vue項(xiàng)目開(kāi)發(fā)常見(jiàn)問(wèn)題和解決方案總結(jié)
這篇文章主要介紹了Vue項(xiàng)目開(kāi)發(fā)常見(jiàn)問(wèn)題和解決方案總結(jié),幫助大家更好的利用vue開(kāi)發(fā),感興趣的朋友可以了解下2020-09-09