Angular 2 延迟加载模块在导航路线时初始化多次?

Angular 2 lazy loaded module initializing multiple times when navigating routes?

使用 RC5

我配置了延迟加载路由:

{path: STATE.PROFILE, loadChildren: 'app/profile/profile.module' },

指向这个模块:

@NgModule({
  imports: [CommonModule, FormsModule, routing],
  declarations: [SelectProfileComponent, ConfirmProfileComponent],
  providers: [ProfileCacheService]
})
export default class ProfileModule { }

...以及这些路线

const routes: Routes = [
  { path: '', component: SelectProfileComponent, canActivate: [ProfileGuard] },
  { path: STATE.CONFIRM, component: ConfirmProfileComponent, canActivate: [ProfileGuard] },
];

在 select-profile 组件中,有一个按钮可以通过 导航到其兄弟路由 "State.Confirm" "shared" 在根 AppModule 中声明的服务。 Angular 路由器有助于在此共享服务中导航:

return this.router.navigateByUrl(url);

问题是有状态存储在 ProfileCacheService 中(在延迟加载模块中声明),但在我导航到 "confirm" 后被清除路线。发生这种情况的原因是因为 模块在尝试导航到同一个延迟加载模块 中的路由时被重新初始化 - 这反过来又重新初始化 CachService 因为它是模块范围的

在根模块上声明 ProfileCacheService 解决了这个问题,但它违背了延迟加载的目的,因为该服务只在该模块中使用。

有没有人运行遇到同样的问题?

我遇到了同样的问题,看起来它已经在 master 中修复并且将成为 rc6 的一部分:

https://github.com/angular/angular/issues/10841