2016-11-17 84 views
2

上不存在在我的Angular 2应用程序中,我使用这里描述的内容提供窗口对象:Angular2 - How to inject window into an angular2 serviceAngular 2 AOT - 属性“窗口”在类型

但是,用于AOT的ngc编译器会返回多个错误。首先,我不得不改变我提供的依赖(注意“窗口”)的方式:

@NgModule({   
    providers: [ 
    { provide: 'Window', useValue: window } 
    ], 
    ... 
}) 
export class AppModule {} 

而且在我的组件(注意类型“任意”):

@Component({ ... }) 
export default class MyComponent { 
    constructor (
     @Inject('Window') private window: any 
    ) {} 
... 

但是我仍然得到由NGC编译器在我的模块ngfactory抛出以下错误:

属性“窗口”上不存在类型

再次,一切工作正常与tsc编译器。

回答

1

下,简单的解决方案为我做的伎俩:

在“@NgModule”,在“供应商“部分:

{provide: 'window', useFactory: getWindow } 

确保导出 ”getWindow“ 的方法:

export function getWindow() { return window; } 

来源 - https://github.com/angular/angular/issues/14050

相关问题