【前端】router.beforeEach导航守卫之权限跳转

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`;
  const userName = localStorage.getItem('userName');
  if (!userName && to.path !== '/login') {
    next('/login');
  } else if (to.meta.permission) {
    userName === 'admin' ? next() : next('/404');
  } else {
    next();
  }
});

猜你喜欢

转载自blog.csdn.net/weixin_43853746/article/details/121773638