如何将窗口注入角度2.1.0 [英] How to Inject Window into Angular 2.1.0

查看:108
本文介绍了如何将窗口注入角度2.1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在早期版本的Angular 2中,我可以通过添加



{提供:Window,useValue:window }

对于提供者数组。



由于升级到最新的稳定版本的角度2(2.1.0)现在抛出控制台错误


compiler.umd.js:14268Uncaught错误:无法解析所有LoginComponent的参数(AuthService,UserMessagesService,?)。


在参数列表中我试图注入Window对象。

解决方案

尝试:

  @NgModule({
声明:[...],
导入:[...],
提供者:[
{provide:windowObject,useValue:window}
]
})

export class HomeModule {}



在您的组件中:

 code>构造函数(@Inject(windowObject)窗口:窗口})


In the earlier RC releases of Angular 2 I was able to inject the window object by adding

{provide: Window, useValue: window}

To the providers array.

Since upgrading to the latest stable release of angular 2 (2.1.0) this now throws a console error

compiler.umd.js:14268Uncaught Error: Can't resolve all parameters for LoginComponent: (AuthService, UserMessagesService, ?).

The ? in the parameter list is where I am trying to inject the Window object.

解决方案

Try with:

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
   { provide: "windowObject", useValue: window}
  ]
})

export class HomeModule {}

in your component:

constructor(@Inject("windowObject") window: Window})

这篇关于如何将窗口注入角度2.1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆