如何在使用提前编译器编译的Angular 2应用程序中动态配置AppModuleNgFactory?
应根据LocationStrategy
环境变量设置 window.isCordova
提供程序类
{
provide: LocationStrategy,
useClass: window.isCordova ? HashLocationStrategy : PathLocationStrategy
}
如果应用程序未使用AOT编译,则无任何问题。但是当使用AOT进行编译时,LocationStrategy
提供程序始终设置为HashLocationStrategy
。
知道怎么做到这一点吗?
答案 0 :(得分:1)
你应该这样做:
{ provide: LocationStrategy, useFactory: locationStrategyFactory, deps: [PlatformLocation] },
locationStrategyFactory 函数应如下所示:
const locationStrategyFactory = (_platformLocation: PlatformLocation) => {
return (isUseHash ? new HashLocationStrategy(_platformLocation) : new PathLocationStrategy(_platformLocation) );
};