路由元

  我们称呼 routes 配置中的每个路由对象为 路由记录。

  一个路由匹配到的所有路由记录会暴露为 $route 对象(还有在导航钩子中的 route 对象)的 $route.matched 数组。

  定义路由的时候可以配置 meta 字段:{...,meta: { requiresAuth: true }}

  文档这个例子太经典了:

 1 router.beforeEach((to, from, next) => {
 2   if (to.matched.some(record => record.meta.requiresAuth)) {
 3     // this route requires auth, check if logged in
 4     // if not, redirect to login page.
 5     if (!auth.loggedIn()) {
 6       next({
 7         path: '/login',
 8         query: { redirect: to.fullPath }
 9       })
10     } else {
11       next()
12     }
13   } else {
14     next() // 确保一定要调用 next()
15   }
16 })

判断要跳转的路由是否需要权限,需要的话判断是否有权限/已登录,未登录就跳转去登陆页

原文地址:https://www.cnblogs.com/alan2kat/p/7345937.html