Vue實(shí)現(xiàn)手機(jī)號(hào)、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí))
最近在做一個(gè)Vue項(xiàng)目,前端通過手機(jī)號(hào)、驗(yàn)證碼登錄,獲取驗(yàn)證碼按鈕需要設(shè)置60s倒計(jì)時(shí)(點(diǎn)擊一次后,一分鐘內(nèi)不得再次點(diǎn)擊)。先看一下效果圖:
輸入正確格式的手機(jī)號(hào)碼后,“獲取驗(yàn)證碼”按鈕方可點(diǎn)擊;點(diǎn)擊“獲取驗(yàn)證碼”后,按鈕進(jìn)入60s倒計(jì)時(shí),效果圖如下:
效果圖已經(jīng)有了,接下來就上代碼吧!
html<el-button @click='getCode()' : :disabled='getCodeBtnDisable'>{{codeBtnWord}}</el-button> 數(shù)據(jù)data
data() { return {loginForm: { phoneNumber: ’’, verificationCode: ’’,},codeBtnWord: ’獲取驗(yàn)證碼’, // 獲取驗(yàn)證碼按鈕文字waitTime:61, // 獲取驗(yàn)證碼按鈕失效時(shí)間 }} 計(jì)算屬性computed
computed: { // 用于校驗(yàn)手機(jī)號(hào)碼格式是否正確 phoneNumberStyle(){let reg = /^1[3456789]d{9}$/if(!reg.test(this.loginForm.phoneNumber)){ return false}return true }, // 控制獲取驗(yàn)證碼按鈕是否可點(diǎn)擊 getCodeBtnDisable:{get(){ if(this.waitTime == 61){if(this.loginForm.phoneNumber){ return false}return true } return true},// 注意:因?yàn)橛?jì)算屬性本身沒有set方法,不支持在方法中進(jìn)行修改,而下面我要進(jìn)行這個(gè)操作,所以需要手動(dòng)添加set(){} }}
關(guān)于上面給計(jì)算屬性添加set方法,可以參照//www.jb51.net/article/202496.htm
css設(shè)置不可點(diǎn)擊時(shí)置灰.el-button.disabled-style { background-color: #EEEEEE; color: #CCCCCC;} mothods中添加獲取驗(yàn)證碼方法
getCode(){ if(this.phoneNumberStyle){let params = {}params.phone = this.loginForm.phoneNumber// 調(diào)用獲取短信驗(yàn)證碼接口axios.post(’/sendMessage’,params).then(res=>{ res = res.data if(res.status==200) {this.$message({ message: ’驗(yàn)證碼已發(fā)送,請(qǐng)稍候...’, type: ’success’, center:true}) }})// 因?yàn)橄旅嬗玫搅硕〞r(shí)器,需要保存this指向let that = thisthat.waitTime--that.getCodeBtnDisable = truethis.codeBtnWord = `${this.waitTime}s 后重新獲取`let timer = setInterval(function(){ if(that.waitTime>1){that.waitTime--that.codeBtnWord = `${that.waitTime}s 后重新獲取` }else{clearInterval(timer)that.codeBtnWord = ’獲取驗(yàn)證碼’that.getCodeBtnDisable = falsethat.waitTime = 61 }},1000) }}
通過上面的代碼,就可以實(shí)現(xiàn)了,如有錯(cuò)誤,敬請(qǐng)指正!
以上就是Vue實(shí)現(xiàn)手機(jī)號(hào)、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí))的詳細(xì)內(nèi)容,更多關(guān)于vue 手機(jī)號(hào)驗(yàn)證碼登錄的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. CSS hack用法案例詳解6. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析7. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向8. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法9. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)10. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法
