亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue實(shí)現(xiàn)驗(yàn)證碼按鈕倒計(jì)時(shí)功能

 更新時(shí)間:2018年04月10日 09:42:41   作者:菜鳥_先菲  
最近項(xiàng)目結(jié)束,空閑時(shí)間比較多,今天小編抽時(shí)間給大家使用vue寫一個(gè)小例子,就決定做驗(yàn)證碼倒計(jì)時(shí)功能,具體實(shí)例代碼大家參考下本文

本人最近開始嘗試學(xué)習(xí)vue.js。想使用vue寫一個(gè)小例子,就選擇做驗(yàn)證碼按鈕倒計(jì)時(shí)功能。

    上網(wǎng)上搜了一下,也把他們的代碼試了一下,自己出了很多問(wèn)題。所以,需要寫一篇基礎(chǔ)入門的文章,避免后面人采坑。

   這是按照網(wǎng)上寫的HTML頁(yè)面

<div class="register-pannel" id ="register-pannel"> 
      <div class="register-l" align="center"> 
        <div class="input-group" style="width: 300px;"> 
          <input type="text" class="form-control" placeholder="郵箱/手機(jī)號(hào)/用戶名" style="height: 40px;" /> 
          <span class="glyphicon glyphicon-user form-control-feedback" aria-hidden="true" /> 
        </div> 
        <br /> 
        <div class="input-group" style="width: 300px;"> 
          <input type="text" class="form-control" placeholder="密碼" style="height: 40px;" /> 
          <span class="glyphicon glyphicon-lock form-control-feedback" /> 
        </div> 
        <br /> 
        <div class="input-group" style="width: 300px;"> 
          <input type="text" class="form-control" placeholder="手機(jī)號(hào)" style="height: 40px;" /> 
          <span class="glyphicon glyphicon-phone form-control-feedback" /> 
        </div> 
        <br /> 
        <div class="input-group" style="width: 300px;"> 
            <span class="register-msg-btn" v-show="show" v-on:click="getCode">發(fā)送驗(yàn)證碼</span> 
            <span class="register-msg-btn" v-show="!show">{{count}} s</span> 
          <input type="text" class="form-control" placeholder="驗(yàn)證碼" style="float: right; height: 40px; width: 150px;" /> 
          <span class="glyphicon glyphicon-font form-control-feedback" /> 
        </div> 
        <br /> 
        <span class="btn-register">注冊(cè)</span> 
      </div> 

js寫成

<script> 
<span style="white-space: pre;">      </span>data(){   
<span style="white-space: pre;">      </span>return {   
<span style="white-space: pre;">        </span>show: true,   
<span style="white-space: pre;">        </span>count: '',   
<span style="white-space: pre;">        </span>timer: null,   
<span style="white-space: pre;">      </span>}  
<span style="white-space: pre;">    </span>},  
<span style="white-space: pre;">    </span>methods:{   
<span style="white-space: pre;">      </span>getCode(){    
<span style="white-space: pre;">        </span>const TIME_COUNT = 60;    
<span style="white-space: pre;">        </span>if (!this.timer) {     
<span style="white-space: pre;">          </span>this.count = TIME_COUNT;     
<span style="white-space: pre;">          </span>this.show = false;     
<span style="white-space: pre;">          </span>this.timer = setInterval(() => {     
<span style="white-space: pre;">            </span>if (this.count > 0 && this.count <= TIME_COUNT) {      
<span style="white-space: pre;">              </span>this.count--;      
<span style="white-space: pre;">            </span>} else {      
<span style="white-space: pre;">              </span>this.show = true;      
<span style="white-space: pre;">              </span>clearInterval(this.timer);      
<span style="white-space: pre;">              </span>this.timer = null;      
<span style="white-space: pre;">            </span>}     
<span style="white-space: pre;">          </span>}, 1000)     
<span style="white-space: pre;">        </span>}   
<span style="white-space: pre;">      </span>}   
<span style="white-space: pre;">    </span>} 
</script> 

發(fā)現(xiàn)瀏覽器一直報(bào)錯(cuò)Uncaught SyntaxError: Unexpected token {

所以按照官方文檔的格式,把js的結(jié)構(gòu)改成

<script> 
    new Vue({ 
      el:'.register-pannel',      
      data:{    
        show:true,   
        timer:null, 
        count:'' 
      },  
      methods:{   
        getCode(){ 
          this.show = false; 
          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)     
          }   
        }   
      } 
    }); 
    </script> 

于是格式是沒有問(wèn)題了,但是樣式并沒有生效。變成了另一個(gè)樣子。

上網(wǎng)上搜了很多。

有說(shuō)是js引用順序的問(wèn)題。

有說(shuō)是將js寫進(jìn)window.onload的。試了一下,發(fā)現(xiàn)都不對(duì)。

后來(lái),在官方文檔中發(fā)現(xiàn)了el屬性:為實(shí)例提供掛載元素。值可以是 CSS 選擇符,或?qū)嶋H HTML 元素,或返回 HTML 元素的函數(shù)。

所以改成

<script>
 new Vue({
  el:'.register-pannel', //注冊(cè)div的class 
  data:{   
  show:true,  
  timer:null,
  count:''
  }, 
  methods:{  
  getCode(){
   this.show = false;
   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)    
   }  
  }  
  }
 });
</script>

效果就出來(lái)了。

總結(jié)

以上所述是小編給大家介紹vue實(shí)現(xiàn)驗(yàn)證碼按鈕倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue中的數(shù)字滾動(dòng)和翻牌器

    vue中的數(shù)字滾動(dòng)和翻牌器

    這篇文章主要介紹了vue中的數(shù)字滾動(dòng)和翻牌器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue?基于?vuedraggable?實(shí)現(xiàn)選中、拖拽、排序效果

    Vue?基于?vuedraggable?實(shí)現(xiàn)選中、拖拽、排序效果

    這篇文章主要介紹了Vue?基于?vuedraggable?實(shí)現(xiàn)選中、拖拽、排序效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 輕量級(jí)富文本編輯器wangEditor結(jié)合vue使用方法示例

    輕量級(jí)富文本編輯器wangEditor結(jié)合vue使用方法示例

    在我們項(xiàng)目中,有些時(shí)候需要使用富文本編輯器。本文將以百度開發(fā)的Ueditor結(jié)合Vue.js介紹一下。非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-10-10
  • Vue表格首列相同數(shù)據(jù)合并實(shí)現(xiàn)方法

    Vue表格首列相同數(shù)據(jù)合并實(shí)現(xiàn)方法

    這篇文章主要介紹了Vue實(shí)現(xiàn)表格首列相同數(shù)據(jù)合并的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • Vue項(xiàng)目中封裝axios的方法

    Vue項(xiàng)目中封裝axios的方法

    這篇文章主要介紹了Vue項(xiàng)目中封裝axios的方法,axios 是一個(gè)輕量的 HTTP客戶端,基于 XMLHttpRequest 服務(wù)來(lái)執(zhí)行 HTTP 請(qǐng)求,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 基于axios請(qǐng)求封裝的vue應(yīng)用實(shí)例代碼

    基于axios請(qǐng)求封裝的vue應(yīng)用實(shí)例代碼

    這篇文章主要給大家介紹了基于axios請(qǐng)求封裝的vue應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 解決在vue項(xiàng)目中,發(fā)版之后,背景圖片報(bào)錯(cuò),路徑不對(duì)的問(wèn)題

    解決在vue項(xiàng)目中,發(fā)版之后,背景圖片報(bào)錯(cuò),路徑不對(duì)的問(wèn)題

    下面小編就為大家分享一篇解決在vue項(xiàng)目中,發(fā)版之后,背景圖片報(bào)錯(cuò),路徑不對(duì)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • 使用use注冊(cè)Vue全局組件和全局指令的方法

    使用use注冊(cè)Vue全局組件和全局指令的方法

    下面小編就為大家分享一篇使用use注冊(cè)Vue全局組件和全局指令的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • webstorm和.vue中es6語(yǔ)法報(bào)錯(cuò)的解決方法

    webstorm和.vue中es6語(yǔ)法報(bào)錯(cuò)的解決方法

    本篇文章主要介紹了webstorm和.vue中es6語(yǔ)法報(bào)錯(cuò)的解決方法,小編總結(jié)了webstorm和.vue中出現(xiàn)的es6語(yǔ)法報(bào)錯(cuò),避免大家采坑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Vue?設(shè)置圖片不轉(zhuǎn)為base64的方式

    Vue?設(shè)置圖片不轉(zhuǎn)為base64的方式

    這篇文章主要介紹了Vue實(shí)現(xiàn)設(shè)置圖片不轉(zhuǎn)為base64的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評(píng)論