您现在的位置是:首页 > Javascript
当前栏目
vue中 router.beforeEach() 的用法
2023-03-31 10:40:30 时间
导航守卫 主要是通过跳转或取消得方式守卫导航
- 在前端路由跳转中,路由跳转前都是会经过beforeEach,而beforeEach可以通过next来控制到底去哪个路由。
- 根据这个特性我们就可以在beforeEach中设置一些条件来控制路由的重定向。
常见的使用场景有:
- 1、验证用户是否登录(若未登录,且当前非登录页面,则自动重定向登录页面);
- 2、用户权限;
- 3、用户输入的路路径是否存在,不存在的情况下如何处理,重定向到哪个页面。
此处呢我使用一个简单的例子:
当用户输入的路径不存在的情况下,将其重定向到‘/’路径来说明beforeEach是如何控制路由的。
话不多说,直接看下边如何实现的(这里我以创建一个名为router-be的项目为例)。
第一步 : 规定进入路由是否需要权限
@/router/index.js
import A from '@/components/a'
{
path : '/a',
name : 'a',
component : A,
meta : { // 加一个自定义obj
requireAuth : true // 参数 true 代表需要登陆才能进入 A
}
}
第二步 : 使用vuex 整一个useid
@/assets/store.js
//使用vuex三步走
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
//这个理论来说
const store = new Vuex.Store({
state:{
userId : ''
}
})
export default store
第三步 : 使用router.beforeEach()
思路:【
如果(进入这个路由需要权限){
如果(能获取到这个用户的userID){
就让这个用户进入这个路由
}否则{
就让这个用户进入b这个页面
}
} 即将进入的路由不需要权限就能进入 {
就让这个用户进入这个路由
}
】
对应代码:
import store from '@/assets/store' //把这个userId获取过来
router.beforeEach((to,from,next)=>{
if(to.meta.requireAuth){
if(store.state.userId){
next()
}else{
next({path:'/b'})
}
}else{
next()
}
})
实现原理
- constrouter=newVueRouter({…})
- router.beforeEach((to,from,next)=>{// …})
- 每个守卫方法接受三个参数 :
1.to => route : 即将进入的目标路由对象
2.from => route : 当前导航正要离开的路由
3.next => function: 一定要调用该方法来 resolve这个钩子,执行效果以来 next 方法的调用参数
第四步
- 上一步 /b 路由为 登陆页面
- 当进入A 页面之前,需要先请求接口,获取是否有登录,然后把userId存在vuex 的state 中
- 当没有userId 时,则在登录之后,存一个userId 到state 里
第五步 项目中使用
router.beforeEach((to, from, next) => {
console.log(to);
// DEBUG: 测试
return next();
const { ldomain } = to.query;
if (ldomain) {
document.domain = ldomain;
}
const { LoginPage } = byskConfig;
if (!router.getMatchedComponents(to.path).length) {
console.log("not page", to, from);
return next(byskConfig.NotFoundPage.path);
}
//验证权限
if (to.meta.permId && !hasPerms(to.meta.permId)) {
console.log("no auth", to, from);
return next(LoginPage.path);
}
//验证是否登录
if (to.meta.permId && !getCookie("sid")) {
console.log("no login & signout", to, from);
return next(LoginPage.path);
}
if (to.matched.length) {
let parentRoute = to.matched[to.matched.length - 1].parent;
if (
parentRoute &&
parentRoute.meta.hasOwnProperty("redirect") &&
parentRoute.meta.redirect !== to.path) {
parentRoute.meta.redirect = to.path;
}
}
next();
});
权限
export function setupPermissionGuard(router) {
router.beforeEach(async (to, from, next) => {
const { state, commit, dispatch } = store;
// TODO: 404 的处理
// 不需要登录,可访问
if (to.meta.ignoreAuth === true) {
next();
return;
}
// TODO: 白名单
// 刷新重新登录
if (!state.appToken) {
await dispatch('relogin');
}
// 处理token
const hasToken = !!state.appToken;
if (!hasToken) { // 未登陆,或过期
// 重定向到登录页
const redirectData = {
path: LOGIN_PATH,
replace: true,
};
next(redirectData);
return;
}
// 判断是否有权限
const hasAuthority = to.meta.permId && hasPerms(to.meta.permId);
if (hasAuthority) { // 权限验证
if (to.meta.ignoreKeepAlive !== true) {
// 页面需要缓存, 添加到缓存
const { cachePages } = state;
const { matched } = to;
commit('setCachePages', [...cachePages, ...matched.slice(1)]);
}
next();
return;
}
next(from.path);
// next();
});
router.afterEach((to) => {
if (to.meta?.label) {
// 设置页面`title`
document.title = `一立科技 - ${to.meta.label}`;
}
});
}
相关文章
- 微软 Teams 2.0 迎来界面更新,适配 Windows 11 云母设计
- JS语言在HarmonyOS应用开发框架中的作用
- HarmonyOS基础之JS UI任意组件通讯
- HarmonyOS实战—ToastDialog组件的基本使用
- SPDX 正式成为国际标准,以解决供应链安全问题
- HarmonyOS PageSlider组件使用(二)Provider介绍
- HarmonyOS实战—CommonDialog组件的基本使用
- 浅谈HarmonyOS Glide组件的GIF能力
- Windows 11更新:带来Microsoft 365小组件 商店界面微调
- 微软 Windows 11 Build 22000.168 (KB5005191) 预览版发布:Teams 更多语言
- HarmonyOS 鸿蒙开源第三方组件-表情雨EmojiRain
- 不是 Bug,微软确认 Windows 11 任务栏不再支持一些功能特性
- Windows 11预览版更新!带你围观Windows 11全新内置功能
- 经典组件在Windows 11获新生!Windows 11新版画图曝光
- HarmonyOS用Matrix实现各种图片ScaleType缩放
- HarmonyOS 基础之 UI 布局(一)
- HarmonyOS列表组件-ListContainer
- HarmonyOS实战—Text组件宽高三种值的写法和颜色属性
- 鸿蒙开源第三方组件—ANR异常监测组件 ANR-WatchDog-ohos
- 鸿蒙开源第三方组件—自定义流式布局组件FlowLayout_ohos