0%

vue-router路由元信息

路由元信息

定义路由的时候可以配置 meta 字段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
chidren: [
{
path: '/bar',
component: Bar,
meta: {
// a meta field
requiresAuth: true
}
}
]
}
]
})

我们称呼 routes 配置中的每个路由对象为路由记录, 路由记录可以是嵌套的, 因此, 当一个路由匹配成功后, 他可能匹配多个路由记录

例如上次面的配置, /for/bar 这个 RUL 将会匹配父路由以及子路由记录

一个路由匹配到的所有路由记录会暴露为 $route 对象(还有在导航守卫中的路由对象)的 $route.matched 数组. 因此, 我们需要遍历 $route.matched 来检查路由记录中的 meta 字段

下面的例子展示在全局导航守卫中检查元字段

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