需求:點(diǎn)擊重置按鈕,重置登陸表單
1.在表單中引入ref="loginFormRef"屬性,loginFormRef值可以自定義;
2.給重置按鈕綁定點(diǎn)擊事件:@click="resetLoginForm";
3.實(shí)現(xiàn)點(diǎn)擊事件觸發(fā)的方法,得到引入的屬性值對(duì)象,調(diào)用resetFields方法,即:this.$refs.loginFormRef.resetFields()
代碼如下:
<template>
<div class="login-container">
<div class="login_box">
<!--頭像區(qū)域-->
<div class="avatar_box">
<img src="../assets/logo.png">
</div>
<!--登陸表單區(qū)域-->
<el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
<!--用戶名-->
<el-form-item prop="username">
<el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
</el-form-item>
<!--密碼-->
<el-form-item prop="password">
<el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-lock"></el-input>
</el-form-item>
<!--按鈕區(qū)域-->
<el-form-item class="btns">
<el-button type="primary">登陸</el-button>
<el-button type="info" @click="resetLoginForm">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data () {
return {
loginForm: {
username: 'li',
password: 'zz'
},
// 1.在需要校驗(yàn)的表單上綁定校驗(yàn)規(guī)則::rules="loginFormRules";
// 2.在data中定義loginFormRules規(guī)則對(duì)象:loginFormRules:{username: [{ required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur' },
// { min: 3, max: 10, message: '長(zhǎng)度在 3 到 10 個(gè)字符', trigger: 'blur' }],password: []};
// 3.在需要校驗(yàn)的具體表單上引入prop="username"屬性。
loginFormRules: {
username: [
{ required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur' },
{ min: 3, max: 10, message: '長(zhǎng)度在 3 到 10 個(gè)字符', trigger: 'blur' }
],
password: [
{ required: true, message: '請(qǐng)輸入密碼', trigger: 'blur' },
{ min: 6, max: 15, message: '長(zhǎng)度在 6 到 15 個(gè)字符', trigger: 'blur' }
]
}
}
},
methods: {
// 點(diǎn)擊重置按鈕,重置登陸表單
// 1.在表單中引入ref="loginFormRef"屬性,loginFormRef值可以自定義;
// 2.給重置按鈕綁定點(diǎn)擊事件:@click="resetLoginForm";
// 3.實(shí)現(xiàn)點(diǎn)擊事件觸發(fā)的方法,得到引入的屬性值對(duì)象,調(diào)用resetFields方法,即:this.$refs.loginFormRef.resetFields()
resetLoginForm () {
this.$refs.loginFormRef.resetFields()
}
}
}
</script>
<style lang="less" scoped>
.login-container {
background-color: #2b4b6b;
height: 100%;
.avatar_box {
height: 130px;
width: 130px;
position: absolute;
left: 50%;
transform: translate(-50%,-50%);
background-color: white;
border: 1px solid #eee;
border-radius: 50%;
padding: 10px;
box-shadow: 0 0 10px #ddd;
img {
height: 100%;
width: 100%;
border-radius: 50%;
background-color: #eee;
}
}
}
.login_form {
position: absolute;
bottom: 0;
width: 100%;
padding: 0 20px;
box-sizing: border-box;
}
.login_box {
background-color: #fff;
height: 300px;
width: 450px;
border-radius: 3px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.btns {
/*尾部對(duì)齊*/
display: flex;
justify-content: flex-end;
}
</style>
?
本文摘自 :https://blog.51cto.com/u