2017-04-20 55 views
1

我有一些问题试图将AoT加装到现有的应用程序中。我收到此错误:无法读取未定义的属性'initialNavigation' - Angular v4 AoT

Cannot read property 'initialNavigation' of undefined 
RouterInitializer.webpackJsonp.1.RouterInitializer.isLegacyDisabled 

这大约是我的AppModule是什么样子:

@NgModule({ 
    imports: [ 
    RouterModule.forRoot(routes, { 
     useHash: true, 
     enableTracing: true, 
     initialNavigation: true, 
    }), 
    ], 
    declarations: [ 
    AppComponent, 
    ], 
    exports: [ 
    AppComponent, 
    ], 
    providers: [ 
    Http, 
    ConfigService, 
    { 
     provide: APP_INITIALIZER, 
     useFactory: configServiceFactory, 
     multi: true, 
     deps: [Http, ConfigService], 
    }, 
    ], 
    bootstrap: [ 
    AppComponent, 
    ], 
}) 
export class AppModule { } 

好像路由器是不确定的,而这个代码路径运行在路由器的选择是不确定的:RouterInitializer.isLegacyDisabled这在路由器模块中从appInitializer调用。

我不知道该怎么做。我觉得在APP_INITIALIZER承诺完成之前,路线正在解决。

这似乎在JIT中工作正常,但AOT复杂化癖本身。如果你需要更多的细节,请让我知道。

编辑:看起来像我可以通过错误,如果我注释掉APP_INITIALIZER。但我注意到路由器也使用它来初始化,有没有办法让路由器等我先初始化?

回答

2

APP_INITIALIZER由于路由器现在挂钩它,导致这种情况。

解决方案是根本不使用它来引导配置,路由器通过将其设置initialNavigation更改为false来手动启动它。

RouterModule.forRoot(routes, { 
    initialNavigation: false, 
}), 

所以我感动APP_INITIALIZER提供商进入app.component.ts

constructor(
    private router: Router, 
    private http: Http, 
    private configService: ConfigService, 
) { 
    // initialise routes once config is loaded 
    configServiceFactory(http, configService).subscribe(() => router.initialNavigation()); 
} 

注:configServiceFactory基本上是这样的:

return http.get('config.json') // or your remote config service 
    .map(m => m.json()) 
    .do(config => configService.setAppConfig(config)) // store it somewhere 
    .catch(err => { 
     console.error('Error getting config for app initialisation', err); 
     return err; 
    }); 
相关问题