首先:
< el-input v-show=“show”>获取验证码</ el-input>
< el-input v-show=“!show”>{{ count }} s后重新获取< /el-input>
1,data里面定义控制的变量
show: true, //控制验证码的显示
count: '', //验证码
timer: null, //时间
2, 在this.调用验证码方法();下面写:
const TIME_COUNT = 60; //倒计时的初始值
if (!this.timer) {
this.count = TIME_COUNT;
this.show = false;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--;
} else {
this.show = true;
clearInterval(this.timer);
this.timer = null;
}
}, 1000)
}