Route Meta Fields

Watch a free video lesson on Vue School

Sometimes, you might want to attach arbitrary information to routes like transition names, who can access the route, etc. This can be achieved through the meta property which accepts an object of properties and can be accessed on the route location and navigation guards. You can define meta properties like this:

  1. const routes = [
  2. {
  3. path: '/posts',
  4. component: PostsLayout,
  5. children: [
  6. {
  7. path: 'new',
  8. component: PostsNew,
  9. // only authenticated users can create posts
  10. meta: { requiresAuth: true }
  11. },
  12. {
  13. path: ':id',
  14. component: PostsDetail
  15. // anybody can read a post
  16. meta: { requiresAuth: false }
  17. }
  18. ]
  19. }
  20. ]

So how do we access this meta field?

First, each route object in the routes configuration is called a route record. Route records may be nested. Therefore when a route is matched, it can potentially match more than one route record.

For example, with the above route config, the URL /posts/new will match both the parent route record (path: '/posts') and the child route record (path: 'new').

All route records matched by a route are exposed on the $route object (and also route objects in navigation guards) as the $route.matched Array. We could loop through that array to check all meta fields, but Vue Router also provides you a $route.meta that is a non-recursive merge of all meta fields from parent to child. Meaning you can simply write

  1. router.beforeEach((to, from) => {
  2. // instead of having to check every route record with
  3. // to.matched.some(record => record.meta.requiresAuth)
  4. if (to.meta.requiresAuth && !auth.isLoggedIn()) {
  5. // this route requires auth, check if logged in
  6. // if not, redirect to login page.
  7. return {
  8. path: '/login',
  9. // save the location we were at to come back later
  10. query: { redirect: to.fullPath },
  11. }
  12. }
  13. })

TypeScript

It is possible to type the meta field by extending the RouteMeta interface:

  1. // typings.d.ts or router.ts
  2. import 'vue-router'
  3. declare module 'vue-router' {
  4. interface RouteMeta {
  5. // is optional
  6. isAdmin?: boolean
  7. // must be declared by every route
  8. requiresAuth: boolean
  9. }
  10. }