Vue中的驗證登錄狀態(tài)的實現(xiàn)方法
Vue項目中實現(xiàn)用戶登錄及token驗證
先說一下我的實現(xiàn)步驟:
- 使用easy-mock新建登錄接口,模擬用戶數(shù)據(jù)
- 使用axios請求登錄接口,匹配賬號和密碼
- 賬號密碼驗證后, 拿到token,將token存儲到sessionStorage中,并跳轉(zhuǎn)到首頁
- 前端每次跳轉(zhuǎn)時,就使用導航守衛(wèi)(vue-router.beforeEach)判斷 sessionStorage 中有無 token ,沒有就跳轉(zhuǎn)到登錄頁面,有則跳轉(zhuǎn)到對應(yīng)路由頁面。
- 注銷后,就清除sessionStorage里的token信息并跳轉(zhuǎn)到登錄頁面
使用easy-mock模擬用戶數(shù)據(jù)
我用的是easy-mock,新建了一個接口,用于模擬用戶數(shù)據(jù):
{ "error_code": 0, "data": [{ "id": '1', "usertitle": "管理員", "username": "admin", "password": "123456", "token": "@date(T)", }, { "id": '2', "usertitle": "超級管理員", "username": "root", "password": "root", "token": "@date(T)", } ] }
login.vue中寫好登陸框:
<template> <div> <p>用戶名:<input type='text' v-model="userName"></p> <p>密碼:<input type='text' v-model="passWord"></p> <button @click="login()">登錄</button> </div> </template> <script> export default { data() { return { userName:'root', passWord:'root' } } } </script>
然后下載axios:npm install axios --save,用來請求剛剛定義好的easy-mock接口:
login(){ const self = this; axios.get('https://easy-mock.com/mock/5c7cd0f89d0184e94358d/museum/login').then(response=>{ var res =response.data.data, len = res.length, userNameArr= [], passWordArr= [], ses= window.sessionStorage; // 拿到所有的username for(var i=0; i<len; i++){ userNameArr.push(res[i].username); passWordArr.push(res[i].password); } console.log(userNameArr, passWordArr); if(userNameArr.indexOf(this.userName) === -1){ alert('賬號不存在!'); }else{ var index = userNameArr.indexOf(this.userName); if(passWordArr[index] === this.passWord){ // 把token放在sessionStorage中 ses.setItem('data', res[index].token); this.$parent.$data.userTitle = res[index].usertitle; //驗證成功進入首頁 this.startHacking ('登錄成功!'); //跳轉(zhuǎn)到首頁 this.$router.push('/index'); // console.log(this.$router); }else{ alert('密碼錯誤!') } } }).catch(err=>{ console.log('連接數(shù)據(jù)庫失敗!') }) }
這一步最重要的是當賬號密碼正確時,把請求回來的token放在sessionStorage中,
配置路由
然后配置路由新加一個meta屬性:
{ path: '/', name: 'login', component: login, meta:{ needLogin: false } }, { path: '/index', name: 'index', component: index, meta:{ needLogin: true } }
判斷每次路由跳轉(zhuǎn)的鏈接是否需要登錄,
導航衛(wèi)士
在main.js中配置一個全局前置鉤子函數(shù):router.beforeEach(),他的作用就是在每次路由切換的時候調(diào)用
這個鉤子方法會接收三個參數(shù):to、from、next。
- to:Route:即將要進入的目標的路由對象,
- from:Route:當前導航正要離開的路由,
- next:Function:個人理解這個方法就是函數(shù)結(jié)束后執(zhí)行什么,先看官方解釋
- 1.next():進行管道中的下一個鉤子。如果全部鉤子執(zhí)行完了,則導航的狀態(tài)就是confirmed(確認的),
- 2.next(false):中斷當前的導航。如果瀏覽器的url改變了(可能是用戶手動或瀏覽器后退按鈕),那么url地址會重置到from路由對應(yīng)的地址。
- 3.next('/')或next({path:'/'}):跳轉(zhuǎn)到一個不同的地址。當前導航被中斷,進入一個新的導航。
用sessionStorage存儲用戶token
//路由守衛(wèi) router.beforeEach((to, from, next)=>{ //路由中設(shè)置的needLogin字段就在to當中 if(window.sessionStorage.data){ console.log(window.sessionStorage); // console.log(to.path) //每次跳轉(zhuǎn)的路徑 if(to.path === '/'){ //登錄狀態(tài)下 訪問login.vue頁面 會跳到index.vue next({path: '/index'}); }else{ next(); } }else{ // 如果沒有session ,訪問任何頁面。都會進入到 登錄頁 if (to.path === '/') { // 如果是登錄頁面的話,直接next() -->解決注銷后的循環(huán)執(zhí)行bug next(); } else { // 否則 跳轉(zhuǎn)到登錄頁面 next({ path: '/' }); } } })
這里用了router.beforeEach vue-router導航守衛(wèi)
每次跳轉(zhuǎn)時都會判斷sessionStorage中是否有token值,如果有則能正常跳轉(zhuǎn),如果沒有那么就返回登錄頁面。
注銷
至此就完成了一個簡單的登錄狀態(tài)了,瀏覽器關(guān)閉后sessionStorage會清空的,所以當用戶關(guān)閉瀏覽器再打開是需要重新登錄的
當然也可以手動清除sessionStorage,清除動作可以做成注銷登錄,這個就簡單了。
loginOut(){ // 注銷后 清除session信息 ,并返回登錄頁 window.sessionStorage.removeItem('data'); this.common.startHacking(this, 'success', '注銷成功!'); this.$router.push('/index'); }
寫一個清除sessionStorag的方法。
一個簡單的保存登錄狀態(tài)的小Demo。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue?使用mescroll.js框架實現(xiàn)下拉加載和上拉刷新功能
這篇文章主要介紹了vue?使用mescroll.js框架?實現(xiàn)下拉加載和上拉刷新功能,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07Vue Element前端應(yīng)用開發(fā)之樹列表組件
本篇隨筆主要介紹樹列表組件和下拉列表樹組件在項目中的使用,以及一個SplitPanel的組件。2021-05-05