全局前置守卫 beforeEach()
beforeEach() 路由守卫,一般用来做一些进入页面的限制;
beforeEach() 有三个参数,to, from, next
to: 即将要进入的目标路由对象;
from: 当前导航正要离开的路由;
next(): 进行管道中的下一个钩子;
路由独享守卫 beforeEnter()
在路由配置上直接定义 beforeEnter (),参数同 beforeEach()相同
用户未登陆只允许访问登录页和注册页
const routes = [
{
//注册页
path: '/register',
name: 'Register',
component: () => import(/* webpackChunkName: "Register" */ '..//views/Register/Register'),
beforeEnter(to, from, next){
const { isLogin } = localStorage;
//登录跳转至首页
isLogin ? next({ name : 'Home'}): next();
}
},
{
//登陆页
path: '/login',
name: 'Login',
component: () => import(/* webpackChunkName: "Login" */