【发布时间】:2018-05-01 14:41:29
【问题描述】:
我的问题是 Angular 模块之一无法在 Safari 浏览器中正常工作。
我有以下代码:
@NgModule({
bootstrap: [ AppComponent ],
imports: [
BrowserAnimationsModule,
BrowserModule.withServerTransition({
appId: 'app'
}),
AppModule,
'navigator' in window && 'serviceWorker' in navigator ? ServiceWorkerModule.register('./ngsw-worker.js') : ServiceWorkerModuleMock
]
})
如您所见,我想有条件地导入模块。当我使用 JIT 编译时,这种机制运行良好。不幸的是,它不适用于 AOT - ServiceWorkerModuleMock 在任何情况下都会被导入。
作为一种解决方案,我尝试使用ngDoBootstrap 方法:
@NgModule({
entryComponents: [ AppComponent ],
imports: [
BrowserAnimationsModule,
BrowserModule.withServerTransition({
appId: 'app'
}),
AppModule,
ServiceWorkerModule.register('./ngsw-worker.js')
]
})
export class BrowserAppModule {
constructor(private injector: Injector) {}
public ngDoBootstrap(appRef: ApplicationRef): void {
// console.log('navigator' in window && 'serviceWorker' in navigator);
appRef.bootstrap(AppComponent);
}
}
但到目前为止,我不知道如何在此处更改 @NgModule 注释中导入的模块。我应该使用哪个类的哪种方法?
也许可以更改Injector 中给定服务的实例?这个解决方案也会让我满意。
【问题讨论】:
-
This link 可能感兴趣。这是 Rangle.io 团队记录的 AOT 限制的汇编
-
try const imports = [your default module] if condition import.push(serviceWorker)
标签: angular typescript dependency-injection service-worker