本篇文章給大家分享的是有關(guān)Vue 中怎么實(shí)現(xiàn)一個(gè)短信驗(yàn)證碼組件,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)公司于2013年成立,先為淶源等服務(wù)建站,淶源等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為淶源企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1、 Vue 組件代碼如下:
Vue.component('timerBtn',{ template: '<button v-on:click="run" :disabled="disabled || time > 0">{{ text }}</button>', props: { second: { type: Number, default: 60 }, disabled: { type: Boolean, default: false } }, data:function () { return { time: 0 } }, methods: { run: function () { this.$emit('run'); }, start: function(){ this.time = this.second; this.timer(); }, stop: function(){ this.time = 0; this.disabled = false; }, setDisabled: function(val){ this.disabled = val; }, timer: function () { if (this.time > 0) { this.time--; setTimeout(this.timer, 1000); }else{ this.disabled = false; } } }, computed: { text: function () { return this.time > 0 ? this.time + 's 后重獲取' : '獲取驗(yàn)證碼'; } } });
2、使用方式:
<timer-btn ref="timerbtn" class="btn btn-default" v-on:run="sendCode" :disabled="disabled" :second="60"></timer-btn>
disabled 建議不要綁定,我們可以通過調(diào)用組件的setDisabled方法來切換按鈕可用狀態(tài);
second 初始值60s 沒特別值可以不綁定;
所以我們可以在HTML頁面這樣:
<timer-btn ref="timerbtn" class="btn btn-default" v-on:run="sendCode" ></timer-btn>
JS這樣:
var vm = new Vue({ el:'#app', methods:{ sendCode:function(){ vm.$refs.timerbtn.setDisabled(true); //設(shè)置按鈕不可用 hz.ajaxRequest("sys/sendCode?_"+$.now(),function(data){ if(data.status){ vm.$refs.timerbtn.start(); //啟動(dòng)倒計(jì)時(shí) }else{ vm.$refs.timerbtn.stop(); //停止倒計(jì)時(shí) } }); }, } });
以上就是Vue 中怎么實(shí)現(xiàn)一個(gè)短信驗(yàn)證碼組件,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:Vue中怎么實(shí)現(xiàn)一個(gè)短信驗(yàn)證碼組件
轉(zhuǎn)載源于:http://jinyejixie.com/article36/pspgpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)公司、、營銷型網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)