vue實(shí)現(xiàn)登陸功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)登陸功能的具體代碼,供大家參考,具體內(nèi)容如下
最近在學(xué)習(xí)vue,發(fā)現(xiàn)了vue的好多坑,比如怎么更好的獲取input框輸入的值而減少獲取dom節(jié)點(diǎn)的消耗 ,以及怎么綁定一個(gè)函數(shù),去執(zhí)行業(yè)務(wù)邏輯。
1、首先說說怎么獲取input框的值
vue中提供了一個(gè)ref屬性,這里需要在html代碼中用ref屬性來綁定這個(gè)input框,再通過js代碼來獲取input的值:
HTML代碼:
<input type="text" v-model="test1" placeholder="請(qǐng)輸入賬號(hào)" maxlength="11" ?ref="input1" >
js代碼:
this.test1 = this.$refs.input1.value;//獲取input1的值,減少獲取dom節(jié)點(diǎn)的消耗?
2、如何利用vue實(shí)現(xiàn)登陸功能:
這里用到@click來綁定一個(gè)點(diǎn)擊事件
html代碼:
<template> ?? ?<div> ?? ??? ?<ul> ?? ??? ??? ?<li> ?? ??? ??? ??? ?<div> ?? ??? ??? ??? ??? ?賬號(hào): ?? ??? ??? ??? ??? ?<input type="text" v-model="test1" placeholder="請(qǐng)輸入賬號(hào)" maxlength="11" onkeyup="value=value.replace(/[^0-9.]/g,'') " ref="input1" @blur="changeName(test1)"><br /> 密碼: ?? ??? ??? ??? ??? ?<input type="password" v-model="test2" placeholder="請(qǐng)輸入密碼" ref="input2" @blur="changeName1(test2)"><br /> 驗(yàn)證碼: ?? ??? ??? ??? ??? ?<input type="text" v-model="test3" placeholder="請(qǐng)?zhí)顚戲?yàn)證碼" ref="input3"> ?? ??? ??? ??? ??? ?<button @click="getYzm">{{codeText}}</button><br /> ?? ??? ??? ??? ?</div> ?? ??? ??? ?</li> ?? ??? ?</ul> ?? ??? ?<div @click="loginBtn()">點(diǎn)擊登錄</div> ?? ?</div> </template>
js代碼:data用來存放數(shù)據(jù),methods中存放上面定義的事件,如click事件。需要注意的是vue定義事件的寫法。
this.$message('請(qǐng)輸入驗(yàn)證碼');//這個(gè)是element提供的一個(gè)消息提示,類似于layer.msg();
<script type="text/javascript"> ?? ?export default { ?? ??? ?data() { ?? ??? ??? ??? ?return { ?? ??? ??? ??? ??? ?test1: '', ?? ??? ??? ??? ??? ?test2: '', ?? ??? ??? ??? ??? ?test3: '', ?? ??? ??? ??? ??? ?codeText: '獲取驗(yàn)證碼', ?? ??? ??? ??? ?} ?? ??? ??? ?}, ? ?? ??? ??? ?methods: { ?? ??? ??? ??? ?//獲取驗(yàn)證碼 ?? ??? ??? ??? ?getYzm() { ?? ??? ??? ??? ??? ?let reg = /^1[0345789][0-9]{9}$/; ?? ??? ??? ??? ??? ?if(this.test1 == '' || this.test2 == "") {//驗(yàn)證當(dāng)用戶輸入的手機(jī)號(hào)為空或者不正確時(shí)不能進(jìn)行獲取驗(yàn)證碼 ?? ??? ??? ??? ??? ??? ?this.$message({ ?? ??? ??? ??? ??? ??? ??? ?message: '手機(jī)號(hào)密碼不能為空!', ?? ??? ??? ??? ??? ??? ??? ?type: 'warning' ?? ??? ??? ??? ??? ??? ?}); ?? ??? ??? ??? ??? ?} else if(!reg.test(this.test1)) { ?? ??? ??? ??? ??? ??? ?this.$message.error("請(qǐng)輸入正確的手機(jī)號(hào)"); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?this.timer(); ?? ??? ??? ??? ??? ??? ?console.log(this.test1); ?? ??? ??? ??? ??? ?} ? ?? ??? ??? ??? ?}, ?? ??? ??? ??? ?timer() {//驗(yàn)證碼倒計(jì)時(shí) ?? ??? ??? ??? ??? ?let num = 60; ?? ??? ??? ??? ??? ?let that = this; ?? ??? ??? ??? ??? ?that.codeText = num + "秒后重新發(fā)送"; ?? ??? ??? ??? ??? ?let time = setInterval(function() { ?? ??? ??? ??? ??? ??? ?if(num == 0) { ?? ??? ??? ??? ??? ??? ??? ?clearInterval(time); ?? ??? ??? ??? ??? ??? ??? ?time = null; ?? ??? ??? ??? ??? ??? ??? ?that.codeText = "重發(fā)驗(yàn)證碼"; ?? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ?num--; ?? ??? ??? ??? ??? ??? ??? ?that.codeText = num + "秒后重新發(fā)送"; ?? ??? ??? ??? ??? ??? ?} ? ?? ??? ??? ??? ??? ?}, 1000); ?? ??? ??? ??? ?}, ?? ??? ??? ??? ?//用戶名的失去光標(biāo)判斷 ?? ??? ??? ??? ?changeName(userName) { ?? ??? ??? ??? ??? ?let name = userName; ?? ??? ??? ??? ??? ?if(name == '' || name == undefined || name == null) { ?? ??? ??? ??? ??? ??? ?// 用戶名為空時(shí),input框獲得焦點(diǎn) ?? ??? ??? ??? ??? ??? ?this.$refs.input1.focus(); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?console.log(name); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?}, ?? ??? ??? ??? ?//密碼框失去光標(biāo)判斷 ?? ??? ??? ??? ?changeName1(pass) { ?? ??? ??? ??? ??? ?if(pass == '' || pass == undefined || pass == null) { ?? ??? ??? ??? ??? ??? ?// 密碼為空時(shí),input框獲得焦點(diǎn) ?? ??? ??? ??? ??? ??? ?this.$refs.input2.focus(); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?console.log(pass); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?}, ?? ??? ??? ??? ?loginBtn() { ?? ??? ??? ??? ??? ?//一般來講,獲取DOM元素,需document.querySelector(".input1")獲取這個(gè)dom節(jié)點(diǎn),然后在獲取input1的值。但是用ref綁定之后,我們就不需要在獲取dom節(jié)點(diǎn)了,直接在上面的input上綁定input1,然后$refs里面調(diào)用就行。然后在javascript里面這樣調(diào)用:this.$refs.input1 ?這樣就可以減少獲取dom節(jié)點(diǎn)的消耗了 ?? ??? ??? ??? ??? ?this.test1 = this.$refs.input1.value;//獲取input1的值,減少獲取dom節(jié)點(diǎn)的消耗 ? ? ?? ??? ??? ??? ??? ?this.test2 = this.$refs.input2.value; ?? ??? ??? ??? ??? ?this.test3 = this.$refs.input3.value; ?? ??? ??? ??? ??? ?if(this.test3 == '') { ?? ??? ??? ??? ??? ??? ?this.$message.error('請(qǐng)輸入驗(yàn)證碼'); ?? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ?this.$message({ ?? ??? ??? ??? ??? ??? ??? ?message: '恭喜你,登陸成功!', ?? ??? ??? ??? ??? ??? ??? ?type: 'success' ?? ??? ??? ??? ??? ??? ?}); ?? ??? ??? ??? ??? ??? ?console.log(this.test1);//打印出input框輸入的值 ?? ??? ??? ??? ??? ??? ?console.log(this.test2); ?? ??? ??? ??? ??? ??? ?console.log(this.test3); ? ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?}, ? ?? ??? ??? ?} ?? ?} </script>
vue安裝element-ui
1.安裝命令:cnpm install element-ui --save
2.在main.js中加入如下代碼:
import ElementUi from 'element-ui' import '../node_modules/element-ui/lib/theme-chalk/index.css' Vue.use(ElementUi)
3. 輸入命令:npm run dev 運(yùn)行項(xiàng)目
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)登陸登出的實(shí)現(xiàn)示例
- Vue2.0 axios前后端登陸攔截器(實(shí)例講解)
- vue+axios新手實(shí)踐實(shí)現(xiàn)登陸的示例代碼
- 詳解用vue.js和laravel實(shí)現(xiàn)微信授權(quán)登陸
- 基于vue-cli3和element實(shí)現(xiàn)登陸頁面
- Vue 前端實(shí)現(xiàn)登陸攔截及axios 攔截器的使用
- 詳解vue2.0+axios+mock+axios-mock+adapter實(shí)現(xiàn)登陸
- 詳解springboot和vue前后端分離開發(fā)跨域登陸問題
- Vue 頁面權(quán)限控制和登陸驗(yàn)證功能的實(shí)例代碼
- vue+koa2實(shí)現(xiàn)session、token登陸狀態(tài)驗(yàn)證的示例
相關(guān)文章
vue.js實(shí)現(xiàn)的全選與全不選功能示例【基于elementui】
這篇文章主要介紹了vue.js實(shí)現(xiàn)的全選與全不選功能,結(jié)合實(shí)例形式分析了vue.js基于elementui實(shí)現(xiàn)全選與全不選功能的相關(guān)頁面渲染、初始化數(shù)據(jù)及功能函數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12vue頁面加載時(shí)的進(jìn)度條功能(實(shí)例代碼)
這篇文章主要介紹了vue頁面加載時(shí)的進(jìn)度條功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01ElementUI嵌套頁面及關(guān)聯(lián)增刪查改實(shí)現(xiàn)示例
本文主要介紹了ElementUI嵌套頁面及關(guān)聯(lián)增刪查改實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格
這篇文章主要介紹了使用VUE組件創(chuàng)建SpreadJS自定義單元格的方法,通常我們使用組件的方式是,在實(shí)例化Vue對(duì)象之前,通過Vue.component方法來注冊(cè)全局的組件,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-01-01