路由-重定向/导航守卫

1.重定向 

  • 字符串形式配置

当访问/uer会重定向到/user/man

const routes = [ // 这里可以随意命名 routes1 也可以
   
    { path: '/', redirect: "/home" },// 重定向的路径一定是下面注册过的路由的某一个 不要乱写没有创建的组件

    { path: '/news',redirect: "/home", name: '新闻', component: () => import('../views/News.vue') },
    { path: '/playing', component: Playing },
    { path: '/home', name: '玩', component: () => import('../views/Home.vue') },
    { path: '/dynamic/:id', component: () => import('../views/Dynamic.vue') },
    {
        path: '/user',
        component: () => import('../views/User.vue'),
        children: [
            { path: '/user/man', component: () => import('../views/Man.vue') },
            { path: 'woman', component: () => import('../views/Woman.vue') },
        ]
    },
]
  •  对象形式配置
const routes = [ // 这里可以随意命名 routes1 也可以
    { path: '/', redirect: { path: '/home' }, },
    { path: '/news', name: '新闻', component: () => import('../views/News.vue') },
    { path: '/playing', component: Playing },
    { path: '/home', name: '玩', component: () => import('../views/Home.vue') },
    { path: '/dynamic/:id', component: () => import('../views/Dynamic.vue') },
    {
        path: '/user',
        component: () => import('../views/User.vue'),
        children: [
            { path: '/user/man', component: () => import('../views/Man.vue') },
            { path: 'woman', component: () => import('../views/Woman.vue') },
        ]
    },
]

2.导航守卫

  • 全局前置守卫
router.beforeEach((to, from) => { // 路由守卫 也叫 路由拦截器 在做登录页面的时候使用,如果没有登陆不能通过输导航进去
    if (to.path === '/news') {
        return false
    }
})

每个守卫方法接收三个参数:

to: Route, 即将要进入的目标 路由对象;
from: Route,当前导航正要离开的路由;
return false,不能进
return true,能进
  • 全局后置守卫

使用场景一般是路由跳转结束取消loading动画

router.afterEach((to, from) => {
    if (to.path === '/news') {
        console.log('我去news了!')
    }
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值