{
path: 'resource-edit',
component: UploadComponent,
data: {
title: '编辑资源',
},
canActivate: [LoginGuard]
},
因为每个路由我都需要加同样的守卫,如果在每个路由中都加canActivate太麻烦了,请问老师该如何写呢?求解答
router.beforeEach(async(to, from, next) => {})
像Vue这样就很方便
我暂时是用的暴力循环,不知道有没有更优雅的写法
const addGuards = (routes: Routes, name: string) => {
routes.forEach((router: Route) => {
if (!router.children) {
router[name] = [LoginGuard]
} else {
addGuards(router.children, name)
}
})
}
addGuards(routes, 'canActivate')