我有一个 angular2 RC6 应用程序,我需要将加载页面推迟到执行某些功能之后。
我有一项服务可以确定 webApi 的 URI。我需要应用程序在尝试加载任何数据之前等待确定此 URL。如果我使用 *ngIf 将主应用程序隐藏在 app.component 中,我会收到路由错误,因为路由器 socket 不存在。
在 RC5/6 之前,我在服务获取 URI 后手动引导应用程序。
最佳答案
如果你使用路由器,你可以使用canActivate
, canActivateChild
.
另见
- https://angular.io/docs/ts/latest/guide/router.html#!#guards
- Resolve a value *before* activating a route in Angular 2
- Angular2 canActivate() calling async function
要在应用程序初始化之前加载一次,您可以使用 APP_INITIALIZER
,如 How to pass parameters rendered from backend to angular2 bootstrap method 中所述。
关于Angular2 - 如何延迟初始加载直到服务完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459002/