在 Angular 2 的早期 RC 版本中,我能够通过添加
来注入(inject)窗口对象{provide: Window, useValue: window}
到提供者数组。
自从升级到 angular 2 (2.1.0) 的最新稳定版本后,现在会抛出一个控制台错误
compiler.umd.js:14268Uncaught Error: Can't resolve all parameters for LoginComponent: (AuthService, UserMessagesService, ?).
那个?在参数列表中是我试图注入(inject) Window 对象的地方。
最佳答案
尝试:
@NgModule({
declarations: [...],
imports: [...],
providers: [
{ provide: "windowObject", useValue: window}
]
})
export class HomeModule {}
在你的组件中:
constructor(@Inject("windowObject") window: Window})
关于angular - 如何将窗口注入(inject) Angular 2.1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40222084/