2016-11-17 92 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

相關問題