vuex實現(xiàn)登錄狀態(tài)的存儲,未登錄狀態(tài)不允許瀏覽的方法
基礎(chǔ)思路就是使用vuex狀態(tài)管理來存儲登錄狀態(tài)(其實就是存一個值,例如token),然后在路由跳轉(zhuǎn)前進(jìn)行登錄狀態(tài)的判斷,可以使用vue-router的全局前置守衛(wèi)beforeEach,也可以使用路由獨享的守衛(wèi)beforeEnter。
導(dǎo)航守衛(wèi)
正如其名,vue-router``` 提供的導(dǎo)航守衛(wèi)主要用來通過跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航。有多種機(jī)會植入路由導(dǎo)航過程中:全局的, 單個路由獨享的, 或者組件級的。 記住參數(shù)或查詢的改變并不會觸發(fā)進(jìn)入/離開的導(dǎo)航守衛(wèi)。你可以通過觀察 $route 對象來應(yīng)對這些變化,或使用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)建好的實例調(diào)用 beforeRouteEnter 守衛(wèi)中傳給 next 的回調(diào)函數(shù)。
全局守衛(wèi)
你可以使用 router.beforeEach注冊一個全局前置守衛(wèi)
const router = new VueRouter({ ... })
router.beforeEach((to, from, next) => {
// ...
})
當(dāng)一個導(dǎo)航觸發(fā)時,全局前置守衛(wèi)按照創(chuàng)建順序調(diào)用。守衛(wèi)是異步解析執(zhí)行,此時導(dǎo)航在所有守衛(wèi) resolve 完之前一直處于 等待中。
每個守衛(wèi)方法接收三個參數(shù):
to: Route:即將要進(jìn)入的目標(biāo) 路由對象
from: Route:當(dāng)前導(dǎo)航正要離開的路由
next: Function:一定要調(diào)用該方法來 resolve 這個鉤子。執(zhí)行效果依賴 next 方法的調(diào)用參數(shù)。
next():進(jìn)行管道中的下一個鉤子。如果全部鉤子執(zhí)行完了,則導(dǎo)航的狀態(tài)就是 confirmed (確認(rèn)的)。
next(false):中斷當(dāng)前的導(dǎo)航。如果瀏覽器的 URL 改變了(可能是用戶手動或者瀏覽器后退按鈕),那么 URL 地址會重置到 from 路由對應(yīng)的地址。
next('/')或者next({ path: '/' }): 跳轉(zhuǎn)到一個不同的地址。當(dāng)前的導(dǎo)航被中斷,然后進(jìn)行一個新的導(dǎo)航。
next(error):(2.4.0+) 如果傳入 next 的參數(shù)是一個 Error 實例,則導(dǎo)航會被終止且該錯誤會被傳遞給 router.onError()注冊過的回調(diào)。
確保要調(diào)用 next 方法,否則鉤子就不會被 resolved。
路由獨享的守衛(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: '請輸入用戶名', trigger: 'blur' },
],
password: [
{ required: true, message: '請輸入密碼', 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('驗證通過');
this.loginAction();
this.$router.push('manage');
}else{
console.log('賬號密碼出錯');
// this.$message.error('賬號密碼出錯');
this.$message({
type: 'error',
message: '賬號密碼出錯'
});
}
console.log('請求地址: ' + base_url);
}else{
console.log('驗證失敗');
return false;
}
})
}
}
}
接下去只要使用路由守衛(wèi)即可
beforeEach使用實例
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(); //后臺驗證session
}else{
next();
}
})
beforeEnter使用實例
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實現(xiàn)登錄狀態(tài)的存儲,未登錄狀態(tài)不允許瀏覽的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-mounted中如何處理data數(shù)據(jù)
這篇文章主要介紹了vue-mounted中如何處理data數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue中EpicEditor和vue-quill-editor的使用詳解
EpicEditor和Vue-quill-editor都是基于Quill.js的富文本編輯器,并且都提供了許多強(qiáng)大的功能,下面我們就來介紹一下二者的具體使用,感興趣的小伙伴可以了解一下2023-11-11
Vue路由跳轉(zhuǎn)與接收參數(shù)的實現(xiàn)方式
這篇文章主要介紹了Vue路由跳轉(zhuǎn)與接收參數(shù)的實現(xiàn)方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下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-08
Vue在頁面數(shù)據(jù)渲染完成之后的調(diào)用方法
今天小編就為大家分享一篇Vue在頁面數(shù)據(jù)渲染完成之后的調(diào)用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue 百度地圖(vue-baidu-map)繪制方向箭頭折線實例代碼詳解
這篇文章主要介紹了vue 百度地圖(vue-baidu-map)繪制方向箭頭折線,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
vue里面v-bind和Props 利用props綁定動態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue里面v-bind和Props 利用props綁定動態(tài)數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

