如何将窗口注入 Angular 2.1.0 [英] How to Inject Window into Angular 2.1.0
本文介绍了如何将窗口注入 Angular 2.1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Angular 2 的早期 RC 版本中,我能够通过添加
In the earlier RC releases of Angular 2 I was able to inject the window object by adding
{provide: Window, useValue: window}
到提供者数组.
自从升级到 angular 2 (2.1.0) 的最新稳定版本后,现在会引发控制台错误
Since upgrading to the latest stable release of angular 2 (2.1.0) this now throws a console error
compiler.umd.js:14268Uncaught 错误:无法解析 LoginComponent 的所有参数:(AuthService, UserMessagesService, ?).
compiler.umd.js:14268Uncaught Error: Can't resolve all parameters for LoginComponent: (AuthService, UserMessagesService, ?).
?在参数列表中是我试图注入 Window 对象的地方.
The ? in the parameter list is where I am trying to inject the Window object.
推荐答案
尝试:
@NgModule({
declarations: [...],
imports: [...],
providers: [
{ provide: "windowObject", useValue: window}
]
})
export class HomeModule {}
在您的组件中:
constructor(@Inject("windowObject") window: Window})
这篇关于如何将窗口注入 Angular 2.1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文