在Angular2中禁用应用程序加载/路由

mpi*_*dor 6 configuration angular2-router angular

我有一个angular2应用程序(RC6版本),其中一些重要的全局参数使用在启动时收集此文件的服务从配置文件加载:

@NgModule({
imports:  [ BrowserModule, FormsModule, HttpModule, AppRoutes, SomeCustomModule ],
declarations: [ AppComponent ]
providers: [
            ConfigurationService,
            {
              provide: APP_INITIALIZER,
              useFactory: (config: ConfigurationService) => () => { 
                             return config.load().then(configParams=> { return true;});      
                          },
              deps: [ConfigurationService, HttpModule],
              multi: true
            }
          ],
bootstrap:[ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

如本片段所示,我使用我称之为ConfigurationRervice的内容,它通过http调用读取配置文件并返回一些参数(作为Promise).如果一切顺利,应用程序将完美加载,并且可以在子模块等中访问这些配置参数.

问题是,如果此配置文件未按预期加载,如何停止应用程序加载子模块?(请注意,AppComponent实质上只是一个路由器插座,重定向到其他路由,由多个子模块定义).我可以"禁用"或重定向到错误页面而不是加载由请求的路由定义的模块/组件吗?

Pie*_*Duc 10

要禁用页面的初始路由,并根据ConfigurationService您必须在对象中设置的内容initialNavigation进行处理:falseAppRoutes

export const AppRoutes = RouterModule.forRoot(ROUTES, {initialNavigation : false});
Run Code Online (Sandbox Code Playgroud)

在你的内部ConfigurationService,然后必须注入router并使用它来导航,具体取决于您的配置.

另一种选择是使用canActivate路由挂钩.在这个钩子中你可以注入ConfigurationService并让它确定它是否可以加载这个页面.