CanActivate AuthGuard被忽略了吗? [英] CanActivate AuthGuard Ignored?
问题描述
我是使用以下路由配置加载books模块( src / app / index.ts
) - 请注意,这是一个stackblitz链接 - 它现在可以通过在答案中实现修复 - 打破它从书籍模块路由中删除authguard:
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:
{
path: 'books',
loadChildren: './books/books.module#BooksModule',
canActivate: [AuthGuard],
},
books模块中的路由( src / app / books / index.ts
)看起来像这样:
The routing in the books module (src/app/books/index.ts
) looks like this:
export const routes: Routes = [
{ path: '', component: CollectionPageComponent },
];
由于某种原因,加载此路线会关闭AuthGuard / CanActivate防护不会触发。它内部有一个日志记录声明,它会在触发时跟踪。
For some reason loading this route switches off the AuthGuard / the CanActivate guard does not trigger. 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 },
];
然后警卫触发。想法?
推荐答案
问题是authguard需要住在你的 BooksModule $ c $里面c>路由定义。
The problem is that the authguard needs to live inside your BooksModule
route definition.
#in books/index.ts
export const routes: Routes = [
{ path: '', component: CollectionPageComponent, canActivate: [AuthGuard] },
];
然后你可以从app / index.ts中删除canActivate
you can then remove the canActivate from app/index.ts
这篇关于CanActivate AuthGuard被忽略了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!