1

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

LocationStrategy应根据window.isCordova环境变量设置提供程序类

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

如果应用程序没有使用 AOT 编译,它可以正常工作。但是当它使用 AOT 编译时,LocationStrategyprovider 总是设置为HashLocationStrategy.

知道如何做到这一点吗?

4

1 回答 1

1

您应该执行以下操作:

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

locationStrategyFactory 函数应如下所示:

const locationStrategyFactory = (_platformLocation: PlatformLocation) => {
  return (isUseHash ?  new HashLocationStrategy(_platformLocation) : new PathLocationStrategy(_platformLocation) );
};
于 2021-07-15T05:42:36.107 回答