简体   繁体   English

CanActivate AuthGuard被忽略了吗?

[英]CanActivate AuthGuard Ignored?

I'm loading a books module with the following routing configuration ( src/app/index.ts ) -- Note that this is a stackblitz link - And it now works by implementing the fix in the answer - to break it remove the authguard from the Books Module routing: 我正在加载一个带有以下路由配置的书籍模块( src/app/index.ts - 请注意,这是一个stackblitz链接 - 它现在可以通过在答案中实现修复来实现 - 打破它从中删除authguard图书模块路由:

{
  path: 'books',
  loadChildren: './books/books.module#BooksModule',
  canActivate: [AuthGuard],
},

The routing in the books module ( src/app/books/index.ts ) looks like this: books模块( src/app/books/index.ts )中的路由如下所示:

export const routes: Routes = [
  { path: '', component: CollectionPageComponent },
];

For some reason loading this route switches off the AuthGuard / the CanActivate guard does not trigger. 由于某种原因,加载此路由会关闭AuthGuard / CanActivate防护不会触发。 There is a logging statement inside it tracking when it triggers. 其中有一个日志记录语句在触发时跟踪。

If the route is commented out like this: 如果路线被注释掉如下:

export const routes: Routes = [
//{ path: '', component: CollectionPageComponent },
];

Then the guard triggers. 然后警卫触发。 Thoughts? 思考?

The problem is that the authguard needs to live inside your BooksModule route definition. 问题是authguard需要存在于BooksModule路由定义中。

#in books/index.ts

export const routes: Routes = [
  { path: '', component: CollectionPageComponent, canActivate: [AuthGuard] },
];

you can then remove the canActivate from app/index.ts 然后,您可以从app / index.ts中删除canActivate

You can only use the canActivate guard with component. 您只能将canActivate防护与组件一起使用。 If you want guard on your lazy-loaded module, use canLoad guard. 如果你想在延迟加载的模块上保护,请使用canLoad guard。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM