html
<el-button type="primary" class="code-but" @click='code'
:disabled="codeDisabled"
>
{{codeMsg}}
</el-button>
在data中定义
js
code() {
this.$refs.loginFormRef.validate(async (valid) => {
if (!valid) return;
const { data: res } = await this.$axios.get('/login/authCode', {
params: { phone: this.loginForm.phone },
});
window.console.log(res);
if (res.code !== 200) {
this.$message.error(res.msg);
} else {
this.$message.success('获取验证码成功');
// 获取验证码倒计时
if (!this.timer) {
this.timer = setInterval(() => {
if (this.countdown > 0 && this.countdown <= 60) {
this.countdown -= 1;
this.codeDisabled = true;
if (this.countdown !== 0) {
const codeMsgs = '重新获取';
this.codeMsg = codeMsgs.concat(this.countdown, 's');
} else {
clearInterval(this.timer);
this.codeMsg = '获取验证码';
this.countdown = 60;
this.timer = null;
this.codeDisabled = false;
}
}
}, 1000);
}
}
});