如何动态设置LocationStrategy类

时间:2016-10-29 17:07:37

标签: angular angular2-aot

如何在使用提前编译器编译的Angular 2应用程序中动态配置AppModuleNgFactory?

应根据LocationStrategy环境变量

设置

window.isCordova提供程序类

{
    provide: LocationStrategy,
    useClass: window.isCordova ? HashLocationStrategy : PathLocationStrategy
}

如果应用程序未使用AOT编译,则无任何问题。但是当使用AOT进行编译时,LocationStrategy提供程序始终设置为HashLocationStrategy

知道怎么做到这一点吗?

1 个答案:

答案 0 :(得分:1)

你应该这样做:

{ provide: LocationStrategy, useFactory: locationStrategyFactory, deps: [PlatformLocation] },

locationStrategyFactory 函数应如下所示:

const locationStrategyFactory = (_platformLocation: PlatformLocation) => {
  return (isUseHash ?  new HashLocationStrategy(_platformLocation) : new PathLocationStrategy(_platformLocation) );
};
相关问题