Vue實(shí)現(xiàn)登錄以及登出詳解
首先先了解一下,我們的效果實(shí)現(xiàn)流程
首先登錄概述及業(yè)務(wù)流程和相關(guān)技術(shù)點(diǎn)
- 錄頁面的布局
- 創(chuàng)建兩個(gè)Vue.js文件
- 一個(gè)我們來做登錄頁和注冊(cè)頁
- 登錄頁面的布局
- 配置路由
- 登錄表單的數(shù)據(jù)綁定
- 登錄表單的驗(yàn)證規(guī)則
- 登錄表單的重置
- 登錄預(yù)驗(yàn)證
- 登錄組件配置彈窗提示
- 登錄成功后的行為
- 將登錄之后的token,保存到客戶端的sessionStorage中
- 通過編程式導(dǎo)航跳轉(zhuǎn)到后臺(tái)主頁,路由地址是默認(rèn)路徑 '/'
- 在我們首頁的登出,組件配置彈窗提示,把我們的token使用removeItem刪除
登錄業(yè)務(wù)流程
1.在登錄頁面輸入用戶名和密碼
2.調(diào)用后臺(tái)接口進(jìn)行驗(yàn)證
3.通過驗(yàn)證之后,根據(jù)后臺(tái)得響應(yīng)狀態(tài)跳轉(zhuǎn)到項(xiàng)目主頁
登錄功能實(shí)現(xiàn)
1.首先我們用路由守衛(wèi)來驗(yàn)證登錄,判斷是否與需要登錄
{ path:'/login', name:"login", component:login, meta:{ login:true } } // 需要登錄的地方定義meta-true 看他需不需要登錄 if(to.matched.some(item=>item.meta.login)){//需要登錄 console.log("需要登錄"); if(isLogin){//1.已經(jīng)登錄,直接通過 if(data.error===400){//后端告訴你,登錄不成功 next({name:'login'}) localStorage.removeItem('token'); return; } if(to.name==='login'){ next({name:'Home'}) }else{ next() } return; } if(!isLogin && to.name==='login'){//2.未登錄,但要去登錄頁 next() } if(!isLogin && to.name !=='login'){//3.未登錄,去的也不是登錄頁 next({name:"login"}) } }else{//不需要登錄直接進(jìn) next() }
2.表單的驗(yàn)證規(guī)則,我們用的是Element的組件庫
在模板中用Element編寫我們的樣式布局
<div class="login-section"> <!-- :rules="rules" --> <el-form label-position="top" label-width="100px" class="demo-ruleForm" :rules="rules" :model="rulesFrom" status-icon ref="ruleFrom" > <el-form-item label="用戶名" prop="name"> <!-- 使用v-model來獲取用戶輸入的名字 --> <el-input type="text" v-model="rulesFrom.name"></el-input> </el-form-item> <el-form-item label="密碼" prop="password"></el-form-item> <!-- 使用v-model來獲取用戶輸入的密碼 --> <el-input type="password" v-model="rulesFrom.password"></el-input> </el-form-item> <el-form-item> <!-- 定義提交事件 --> <el-button type="primary" @click="submitFrom('ruleFrom')">提交</el-button> <el-button>重置</el-button> </el-form-item> </el-form> </div>
定義表單的驗(yàn)證規(guī)則
詳細(xì)的看Element官網(wǎng)from表單
在Data里面定義
rulesFrom:{ name:'', password:'' }, rules:{ name:[ // 驗(yàn)證規(guī)則 {required:true,message:'請(qǐng)輸入用戶名',trigger:'blur'}, {min:1,max:5,message:'長度在1到5個(gè)字符',trigger:'blur'} ], password:[ {required:true,message:'請(qǐng)輸入密碼',trigger:'blur'}, {min:1,max:5,message:'長度在1到5個(gè)字符',trigger:'blur'} ] }
在methods定義提交事件
// 當(dāng)我們點(diǎn)擊提交的時(shí)候能出發(fā)方法能拿到表單的所有東西 submitFrom(formName){ this.$refs[formName].validate( (valid)=>{ if(valid){ // 如果校檢通過,再里向后端返送用戶信息和密碼 login({ name:this.rulesFrom.name, password:this.rulesFrom.password, }).then((data)=>{ console.log(data); if(data.code===0){ localStorage.setItem('token',data.data.token) window.location.href='/'; } if(data.code===1){ this.$message.error(data.mes) } }) }else{ console.log('error submit!!'); return false } }) }
這個(gè)時(shí)候把登出也寫一下在router beforeEach中給他轉(zhuǎn)換
const token=localStorage.getItem('token'); // ??!token轉(zhuǎn)換成布爾類型 const isLogin=!!token; // 進(jìn)入路由的時(shí)候,需要向后端返送token,驗(yàn)證是否合法 const data=await userInfo(); Store.commit('chageUserInfo',data.data)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue-cli構(gòu)建項(xiàng)目下使用微信分享功能
這篇文章主要介紹了vue-cli構(gòu)建項(xiàng)目下使用微信分享功能,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05Element-ui之ElScrollBar組件滾動(dòng)條的使用方法
這篇文章主要介紹了Element-ui之ElScrollBar組件滾動(dòng)條的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09element中使用formdata進(jìn)行上傳文件的方法
本文主要介紹了elementUI中使用formdata進(jìn)行上傳文件的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03