在我的 Angular 2 应用程序中,我使用此处描述的内容提供了 window 对象:Angular2 - How to injection window into an angular2 service。
然而,用于 AOT 的 ngc 编译器会返回几个错误。首先,我必须更改提供依赖项的方式(注意“窗口”):
@NgModule({
providers: [
{ provide: 'Window', useValue: window }
],
...
})
export class AppModule {}
在我的组件中(注意类型'any'):
@Component({ ... })
export default class MyComponent {
constructor (
@Inject('Window') private window: any
) {}
...
但是,我的模块 ngfactory 中的 ngc 编译器仍然抛出以下错误:
类型上不存在属性“窗口”
再次使用 tsc 编译器一切正常。