Route Meta Fields | vue get router meta
Sometimes,youmightwanttoattacharbitraryinformationtorouteslike:transitionnames,orrolestocontrolwhocanaccesstheroute,etc.Thiscanbeachievedthroughthemetapropertywhichacceptsanobjectofpropertiesandcanbeaccessedontheroutelocationandnavigationguards.Youcandefinemetapropertieslikethis:jsconstroutes=[{path:/posts,component:PostsLayout,children:[{path:new,component:PostsNew,//onlyauthenticateduserscancreatepostsmeta:{requiresAuth:true},},{path::id,component:PostsDetail,//anybodycanreadapostmeta:{re...
Sometimes, you might want to attach arbitrary information to routes like: transition names, or roles to control 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:
jsconst routes = [ { path: /posts, component: PostsLayout, children: [ { path: new, component: PostsNew, // only authenticated users can create posts meta: { requiresAuth: true }, }, { path: :id, component: PostsDetail, // anybody can read a post meta: { requiresAuth: false }, }, ], }, ]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, wi...