【问题标题】:CanActivate AuthGuard Ignored?CanActivate AuthGuard 被忽略?
【发布时间】:2019-01-02 16:09:51
【问题描述】:

I'm loading a books module with the following routing configuration (src/app/index.ts) -- 请注意,这是一个 stackblitz 链接 - 现在它通过在答案中实施修复来工作 - 打破它从 Books Module 路由中删除 authguard:

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

books 模块 (src/app/books/index.ts) 中的路由如下所示:

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

由于某种原因,加载此路由会关闭 AuthGuard / CanActivate 防护不会触发。它里面有一个日志记录语句来跟踪它什么时候触发。

如果路由被这样注释掉:

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

然后守卫触发。想法?

【问题讨论】:

  • 你能做一个stackblitz吗?在我的脑海中,有些东西可能会被覆盖。此外,请确保您没有两次包含相同的路由路径。
  • 链接是 Stacblitz ...我现在在它旁边放了一个注释,以便更清楚。

标签: javascript angular typescript


【解决方案1】:

问题在于 authguard 需要存在于您的 BooksModule 路由定义中。

#in books/index.ts

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

然后您可以从 app/index.ts 中删除 canActivate

【讨论】:

    【解决方案2】:

    您只能将canActivate 防护与组件一起使用。如果你想保护你的延迟加载模块,请使用canLoadguard。

    【讨论】:

    • 我是从 angular 4 开始学习的,从那以后就一直存在,不确定在那之前是否存在。而且我猜你发布了一些错误的演示链接(你已经分享了“通用汽车的衰落真正开始于其将人变成机器的追求”的链接,不能将它与角度联系起来)
    • 哦 - 糟糕 - 感谢提醒 - 这是正确的链接:stackblitz.com/edit/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2017-05-09
    相关资源
    最近更新 更多