Vue路由守衛(wèi)及頁(yè)面登錄權(quán)限控制的設(shè)置方法(兩種)
①先在我們的登錄頁(yè)面存儲(chǔ)一個(gè)登錄數(shù)據(jù)
// 登錄成功時(shí)保存一個(gè)登錄狀態(tài);sessionStorage.setItem('flag', 1);
② 添加路由守衛(wèi)
方法一: 直接在路由中添加
const router = new VueRouter({ ... }) // 路由守衛(wèi) router.beforeEach((to, from, next) => { // ...})
方法二:當(dāng)我們使用的是export default 方法時(shí)可以在main.js中添加 router.beforeEach((to, from, next) => { })方法。
const Recruit = resolve => require([’../components/common/main/index.vue’], resolve);export default new Router({ routes: [ // 登錄 { path: path.login.path, name: path.login.path, component: Login, }, .........
③ 在路由當(dāng)中添加自定義字段requireAuth,判斷當(dāng)前路由是否需要登錄。
下圖中1是設(shè)置多權(quán)限時(shí)的設(shè)置方法,下圖中2是單權(quán)限設(shè)置方法
④ 在路由守衛(wèi)中添加我們自己的代碼邏輯。
// 路由守衛(wèi) router.beforeEach((to,from,next)=>{ let flag = sessionStorage.getItem(’flag ’) if(to.meta.requireAuth == true){ // 需要登錄權(quán)限進(jìn)入的路由 if(!flag){ // 獲取不到登錄信息 next({ path: ’/login’ }) }else{ // 獲取到登錄信息,進(jìn)行下一步 return next(); } }else{ // 不需要登錄權(quán)限的路由直接進(jìn)行下一步 return next(); }})
總結(jié)
到此這篇關(guān)于Vue路由守衛(wèi)及頁(yè)面登錄權(quán)限控制的設(shè)置方法的文章就介紹到這了,更多相關(guān)vue 路由守衛(wèi)頁(yè)面登錄權(quán)限內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python爬蟲(chóng)beautifulsoup解析html方法2. python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能3. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以?xún)?nèi))4. Python基礎(chǔ)之numpy庫(kù)的使用5. 詳解Python模塊化編程與裝飾器6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. python web框架的總結(jié)8. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式9. css進(jìn)階學(xué)習(xí) 選擇符10. 解決python logging遇到的坑 日志重復(fù)打印問(wèn)題
