vue-route路由元信息meta

配置路由时可以配置meta字段,这个字段是元字段。
我们称routes配置中的每个路由对象为路由记录;路由记录是可以嵌套的,因此当一个路由匹配成功后,他可能有多个路由记录。
例如,下面这个例子将匹配父路由记录以及子路由记录。

const router=new VueRouter({
 routes:[
  {
   path:'/foo',
   component:Foo,
   children:[
    {
     path:'bar',
     cpmponent:Bar,
     meta:{ requiresAuth:true }//a meta field
    }
   ]
  }
 ]
})

一个路由匹配到的所有路由记录都会暴露为 r o u t e route.matched数组;因此,我们要遍历matched数组中的$route.matched来检查路由记录中的meta字段

route.beforeEach((to,from,next)=>{
 if(to.matched.some(record => record.meta.requiredAuth)){
  if(!auth.loggedIn()){
   next({
    path:'/login',
    query:{ redirect:to.fullPath }
   })
  }else{
   next()
  }else{
   next()//确保一定要调用next()
  }
 }
}
)

猜你喜欢

转载自blog.csdn.net/E_li_na/article/details/80222912