【问题标题】:How to path prefix from lazy loaded module components route如何从延迟加载的模块组件路由中获取路径前缀
【发布时间】:2020-07-24 13:26:05
【问题描述】:

我有一个延迟加载功能模块 calendar.module 加载在app-routing.module 之类的

{
  path: "calendars",
  canActivate: [AuthGuard],
  loadChildren: () =>
    import("./calendars/calendar.module").then((m) => m.CalendarModule),
}

现在,我在calendar.module 中有一个CraftMapComponent

我想在没有像这样的 日历 前缀的情况下访问这个组件。

http://localhost:4200/teacher/craft-map

但我在calendar-routing.ts 中所能做的就是添加类似的路线

const routes: Routes = [
  { path: '', component: CalendarComponent, children: [
    { path: 'teacher/craft-map', component: CraftMapComponent }
  ]}
]; 

const routes: Routes = [
  { path: '', component: CalendarComponent },
  { path: 'teacher/craft-map', component: CraftMapComponent }
];

但两者都会解决

http://localhost:4200/calendar/teacher/craft-map

有什么方法可以让我省略日历前缀并仍然使用延迟加载模块

【问题讨论】:

  • in path string 你让写你想要的
  • @TomaszVizaint:谢谢。我在问是否有任何方法可以覆盖该“前缀”
  • 是的,这取决于您的 routerLink,因为如果在路径中您在 routerLink 中使用 {path: 'mylink', ....} 简单地写入 routerLink="/mylink",

标签: angular angular-routing


【解决方案1】:

我认为没有办法做到这一点,因为craftmap 的路由是calendar 路由的子路由。

这就像遍历一棵树,但你想直接跳到一个后代,而不是先遍历它的祖先。

另外,请注意,您可能不想将 CanActivate 与延迟加载的模块一起使用,因为对于 CanActivate,虽然它可能返回 false,但模块会以任何一种方式加载,这可能不是预期的行为.我推荐使用CanLoad,因为如果它失败了(例如返回false),你肯定会知道模块不会被加载。

【讨论】:

  • 谢谢。我想那是没有办法的。我正在预加载延迟加载的模块。因此canActivate。无论如何它都会被加载
猜你喜欢
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
相关资源
最近更新 更多