vuex實(shí)現(xiàn)登錄狀態(tài)的存儲(chǔ),未登錄狀態(tài)不允許瀏覽的方法
基礎(chǔ)思路就是使用vuex狀態(tài)管理來存儲(chǔ)登錄狀態(tài)(其實(shí)就是存一個(gè)值,例如token),然后在路由跳轉(zhuǎn)前進(jìn)行登錄狀態(tài)的判斷,可以使用vue-router的全局前置守衛(wèi)beforeEach,也可以使用路由獨(dú)享的守衛(wèi)beforeEnter。
導(dǎo)航守衛(wèi)
正如其名,vue-router``` 提供的導(dǎo)航守衛(wèi)主要用來通過跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航。有多種機(jī)會(huì)植入路由導(dǎo)航過程中:全局的, 單個(gè)路由獨(dú)享的, 或者組件級(jí)的。 記住參數(shù)或查詢的改變并不會(huì)觸發(fā)進(jìn)入/離開的導(dǎo)航守衛(wèi)。你可以通過觀察 $route 對(duì)象來應(yīng)對(duì)這些變化,或使用beforeRouteUpdate的組件內(nèi)守衛(wèi)。
完整的導(dǎo)航解析流程
1、導(dǎo)航被觸發(fā)。
2、在失活的組件里調(diào)用離開守衛(wèi)。
3、調(diào)用全局的 beforeEach 守衛(wèi)。
4、在重用的組件里調(diào)用 beforeRouteUpdate 守衛(wèi) (2.2+)。
5、在路由配置里調(diào)用 beforeEnter。
6、解析異步路由組件。
7、在被激活的組件里調(diào)用 beforeRouteEnter。
8、調(diào)用全局的 beforeResolve 守衛(wèi) (2.5+)。
9、導(dǎo)航被確認(rèn)。
10、調(diào)用全局的 afterEach 鉤子。
11、觸發(fā) DOM 更新。
12、用創(chuàng)建好的實(shí)例調(diào)用 beforeRouteEnter 守衛(wèi)中傳給 next 的回調(diào)函數(shù)。
全局守衛(wèi)
你可以使用 router.beforeEach注冊一個(gè)全局前置守衛(wèi)
const router = new VueRouter({ ... }) router.beforeEach((to, from, next) => { // ... })
當(dāng)一個(gè)導(dǎo)航觸發(fā)時(shí),全局前置守衛(wèi)按照創(chuàng)建順序調(diào)用。守衛(wèi)是異步解析執(zhí)行,此時(shí)導(dǎo)航在所有守衛(wèi) resolve 完之前一直處于 等待中。
每個(gè)守衛(wèi)方法接收三個(gè)參數(shù):
to: Route:即將要進(jìn)入的目標(biāo) 路由對(duì)象
from: Route:當(dāng)前導(dǎo)航正要離開的路由
next: Function:一定要調(diào)用該方法來 resolve 這個(gè)鉤子。執(zhí)行效果依賴 next 方法的調(diào)用參數(shù)。
next():進(jìn)行管道中的下一個(gè)鉤子。如果全部鉤子執(zhí)行完了,則導(dǎo)航的狀態(tài)就是 confirmed (確認(rèn)的)。
next(false):中斷當(dāng)前的導(dǎo)航。如果瀏覽器的 URL 改變了(可能是用戶手動(dòng)或者瀏覽器后退按鈕),那么 URL 地址會(huì)重置到 from 路由對(duì)應(yīng)的地址。
next('/')或者next({ path: '/' }): 跳轉(zhuǎn)到一個(gè)不同的地址。當(dāng)前的導(dǎo)航被中斷,然后進(jìn)行一個(gè)新的導(dǎo)航。
next(error):(2.4.0+) 如果傳入 next 的參數(shù)是一個(gè) Error 實(shí)例,則導(dǎo)航會(huì)被終止且該錯(cuò)誤會(huì)被傳遞給 router.onError()注冊過的回調(diào)。
確保要調(diào)用 next 方法,否則鉤子就不會(huì)被 resolved。
路由獨(dú)享的守衛(wèi)
你可以在路由配置上直接定義beforeEnter守衛(wèi):
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... } } ] })
還有其他部分守衛(wèi),詳情可以看官方文檔https://router.vuejs.org/zh-cn/advanced/navigation-guards.html
安裝vuex后
創(chuàng)建store.js
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); const state = { isLogin: 0 } const mutations = { changeLogin(state,status){ state.isLogin = status; } } const actions = { loginAction({commit}){ commit('changeLogin',1); } } export default new Vuex.Store({ state, actions, mutations })
login.vue中
引入import { mapActions,mapState } from 'vuex'
接著進(jìn)行登錄狀態(tài)的改變,base_url就是路徑
export default { name: 'Login', data(){ return{ loginForm: { username: '', password: '', }, rules: { username: [ { required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur' }, ], password: [ { required: true, message: '請(qǐng)輸入密碼', trigger: 'blur' } ], }, showLogin: false } }, mounted(){ this.showLogin = true; }, computed: { ...mapState(['isLogin']) }, methods: { ...mapActions(['loginAction']), submitForm(formName){ this.$refs[formName].validate((valid) => { if(valid){ if(this.loginForm.username == 'aaa' && this.loginForm.password == '111'){ console.log('驗(yàn)證通過'); this.loginAction(); this.$router.push('manage'); }else{ console.log('賬號(hào)密碼出錯(cuò)'); // this.$message.error('賬號(hào)密碼出錯(cuò)'); this.$message({ type: 'error', message: '賬號(hào)密碼出錯(cuò)' }); } console.log('請(qǐng)求地址: ' + base_url); }else{ console.log('驗(yàn)證失敗'); return false; } }) } } }
接下去只要使用路由守衛(wèi)即可
beforeEach使用實(shí)例
router.beforeEach((to,from,next)=>{ if(to.meta.check){ var check = async function(){ const result = await checkUser(); if(result.status == 0){ next(); }else{ alert('用戶未登錄'); next({path: '/login'}); } } check(); //后臺(tái)驗(yàn)證session }else{ next(); } })
beforeEnter使用實(shí)例
export default new Router({ routes: [ { path: '/login', component: Login }, { path: '/manage', name: '', component: Manage, beforeEnter: (to,from,next)=> { //導(dǎo)航守衛(wèi) console.log(to) console.log(from) if(store.state.isLogin == 1){ console.log('用戶已經(jīng)登錄'); next(); }else{ console.log('用戶未登錄'); next({path: '/login',query:{ Rurl: to.fullPath}}); //未登錄則跳轉(zhuǎn)到登陸界面,query:{ Rurl: to.fullPath}表示把當(dāng)前路由信息傳遞過去方便登錄后跳轉(zhuǎn)回來 } } } ] })
以上這篇vuex實(shí)現(xiàn)登錄狀態(tài)的存儲(chǔ),未登錄狀態(tài)不允許瀏覽的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-mounted中如何處理data數(shù)據(jù)
這篇文章主要介紹了vue-mounted中如何處理data數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue中EpicEditor和vue-quill-editor的使用詳解
EpicEditor和Vue-quill-editor都是基于Quill.js的富文本編輯器,并且都提供了許多強(qiáng)大的功能,下面我們就來介紹一下二者的具體使用,感興趣的小伙伴可以了解一下2023-11-11Vue路由跳轉(zhuǎn)與接收參數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了Vue路由跳轉(zhuǎn)與接收參數(shù)的實(shí)現(xiàn)方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04關(guān)于vue.extend和vue.component的區(qū)別淺析
最近工作中遇到了vue.extend,vue.component,但二者之間的區(qū)別與聯(lián)系是什么呢?下面這篇文章主要給大家介紹了關(guān)于vue.extend和vue.component區(qū)別的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08Vue在頁面數(shù)據(jù)渲染完成之后的調(diào)用方法
今天小編就為大家分享一篇Vue在頁面數(shù)據(jù)渲染完成之后的調(diào)用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue 百度地圖(vue-baidu-map)繪制方向箭頭折線實(shí)例代碼詳解
這篇文章主要介紹了vue 百度地圖(vue-baidu-map)繪制方向箭頭折線,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04vue里面v-bind和Props 利用props綁定動(dòng)態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue里面v-bind和Props 利用props綁定動(dòng)態(tài)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08