Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)
最近在做一個Vue項目,前端通過手機號、驗證碼登錄,獲取驗證碼按鈕需要設(shè)置60s倒計時(點擊一次后,一分鐘內(nèi)不得再次點擊)。先看一下效果圖:

輸入正確格式的手機號碼后,“獲取驗證碼”按鈕方可點擊;點擊“獲取驗證碼”后,按鈕進入60s倒計時,效果圖如下:


效果圖已經(jīng)有了,接下來就上代碼吧!
- html
<el-button @click="getCode()" :class="{'disabled-style':getCodeBtnDisable}" :disabled="getCodeBtnDisable">{{codeBtnWord}}</el-button>
- 數(shù)據(jù)data
data() {
return {
loginForm: {
phoneNumber: '',
verificationCode: '',
},
codeBtnWord: '獲取驗證碼', // 獲取驗證碼按鈕文字
waitTime:61, // 獲取驗證碼按鈕失效時間
}
}
- 計算屬性computed
computed: {
// 用于校驗手機號碼格式是否正確
phoneNumberStyle(){
let reg = /^1[3456789]\d{9}$/
if(!reg.test(this.loginForm.phoneNumber)){
return false
}
return true
},
// 控制獲取驗證碼按鈕是否可點擊
getCodeBtnDisable:{
get(){
if(this.waitTime == 61){
if(this.loginForm.phoneNumber){
return false
}
return true
}
return true
},
// 注意:因為計算屬性本身沒有set方法,不支持在方法中進行修改,而下面我要進行這個操作,所以需要手動添加
set(){}
}
}
關(guān)于上面給計算屬性添加set方法,可以參照//www.fzitv.net/article/202496.htm
- css設(shè)置不可點擊時置灰
.el-button.disabled-style {
background-color: #EEEEEE;
color: #CCCCCC;
}
- mothods中添加獲取驗證碼方法
getCode(){
if(this.phoneNumberStyle){
let params = {}
params.phone = this.loginForm.phoneNumber
// 調(diào)用獲取短信驗證碼接口
axios.post('/sendMessage',params).then(res=>{
res = res.data
if(res.status==200) {
this.$message({
message: '驗證碼已發(fā)送,請稍候...',
type: 'success',
center:true
})
}
})
// 因為下面用到了定時器,需要保存this指向
let that = this
that.waitTime--
that.getCodeBtnDisable = true
this.codeBtnWord = `${this.waitTime}s 后重新獲取`
let timer = setInterval(function(){
if(that.waitTime>1){
that.waitTime--
that.codeBtnWord = `${that.waitTime}s 后重新獲取`
}else{
clearInterval(timer)
that.codeBtnWord = '獲取驗證碼'
that.getCodeBtnDisable = false
that.waitTime = 61
}
},1000)
}
}
通過上面的代碼,就可以實現(xiàn)了,如有錯誤,敬請指正!
以上就是Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)的詳細(xì)內(nèi)容,更多關(guān)于vue 手機號驗證碼登錄的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Vue中l(wèi)ocalstorage和sessionstorage的使用
這篇文章主要介紹了詳解Vue中l(wèi)ocalstorage和sessionstorage的使用方法和經(jīng)驗心得,有需要的朋友跟著小編參考學(xué)習(xí)下吧。2017-12-12
vue 使用iView組件中的Table實現(xiàn)定時自動滾動效果
要在css中設(shè)置table的高度,使數(shù)據(jù)過多時出現(xiàn)滾動條,將縱向設(shè)置為overflow-y: auto;橫向設(shè)置隱藏 overflow-x: hidden,接下來通過本文介紹vue使用iView組件中的Table實現(xiàn)定時自動滾動效果,需要的朋友可以參考下2024-05-05
vue3+vite+ts?通過svg-sprite-loader?插件使用自定義圖標(biāo)的詳細(xì)步驟
這篇文章主要介紹了vue3+vite+ts通過svg-sprite-loader插件使用自定義圖標(biāo),本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
Vue v2.4中新增的$attrs及$listeners屬性使用教程
這篇文章主要給大家介紹了關(guān)于Vue v2.4中新增的$attrs及$listeners屬性的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

