首页 文章

Angular 2 RC5子路由重新加载包含组件和模块

提问于
浏览
1

角度2有问题重新初始化整个应用程序比我认为它在路线导航时更多 . 创建一个快速演示我复制了角度2路由教程plunker来揭露问题:

http://plnkr.co/edit/wAjaIAva0caufEZJG36s?p=preview

在这个熟悉的演示中,危机中心路线是危机中心模块的延迟装载 . 我在危机中心页面上添加了一些“小部件”占位符 . 如果您单击“危机中心”链接,然后使用“添加小部件”链接增加几次,然后单击其中一个子列表,您会注意到两个小部件计数器已重新初始化 .

危机中心模块,它提供的危机服务以及危机中心组件似乎都完全重新初始化了每次路线变化!对于简单的系统,这可能不是问题,但是与危机中心模块提供的危机服务相关的任何重要状态数据(例如,API会话)都会在每次导航时丢失,不像任何数据app.module提供的app.service .

考虑这个应用程序布局,这更接近我的实际问题应用程序:

|- /app
|  |- app.component
|  |- app.module
|  |- app.routing                     <-- app routes, including lazy load for feature.module for all /feature routes (loadChildren)
|  |- app-api.service                 <-- provided by app.module, handles master auth
|  |- /feature
|  |  |- /child-feature
|  |  |  |- child-feature-1.component <-- displays in feature.component's router-outlet
|  |  |  '- child-feature-2.component <-- displays in feature.component's router-outlet
|  |  |- feature-api.service          <-- this feature's api, provided by feature.module
|  |  |- feature-container.component  <-- has a router-outlet for child-features
|  |  |- feature-menu.component       <-- displays in feature.component
|  |  |- feature-status.component     <-- displays in feature.component
|  |  |- feature.module               <-- provides feature-api.service
|  |  '- feature.routing              <-- feature routes, loaded in feature.module
|  |- /other-feature
|  |  |- other-feature.component
|  |  |- other-feature-list.component
|  |  |- other-feature-detail.component
|  |  |- other-feature.module
|  |  '- other-feature.routing
|  '- main   
|- /node_modules
|- /typings
|- index.html
|- systemjs.config.js
'- tsconfig.json

在我的应用程序中,'feature.module'需要对特定api进行身份验证,但在应用程序的任何其他区域都不需要此身份验证 . 因此,我只想在该模块中加载api服务,使其与应用程序的其余部分隔离 . 在演示中,这个api在每个路线导航之间重新初始化 . 此外,功能容器中路由器插座外的功能菜单和功能状态也会在每个路径导航中重新初始化 .

这看起来非常违反直觉 - 不应该只改变子导航中的内部路由器插座组件?此外,一旦加载了一个模块(以及它提供的任何服务),它们不应该成为整个应用程序的一部分而不是在每个路径导航上重新初始化吗?

我想我的问题是,“角度2应该有这种行为吗?”如果答案是“是”,那么“我应该如何在子模块中保留有状态服务?”还有“有没有办法强制导航只适用于最里面的路由器插座(不使用指定的插座)?”

非常感谢甚至阅读这本中篇小说,并且非常感谢任何意见 . 重写了这个问题,希望能让它更清晰;)

1 回答

  • 1

    如果有人遇到这两个问题,则两者都是RC6中的 completely fixed . 不确定在RC5期间没有人对此做出更大的讨论,但至少它现在已经解决了:)

相关问题