2017-03-06 46 views
0

我有一個組件作爲休耕:無法運行因緣測試用例由於ngOnDestroy

export class InkbarComponent implements AfterViewInit, OnDestroy { 
    resizeListener: any; 

    constructor(private _renderer: Renderer, private _elementRef: ElementRef, public renderer: Renderer) { } 
    ngAfterViewInit() { 
     this.resizeListener = this.renderer.listenGlobal('window', 'resize', (event) => { 
      if (this.currentElement) { 
       this.switchPostion(this.currentElement); 
      } 
     }); 
    } 
    public switchPostion(element: HTMLElement) { 
     // function logic 
    } 


    ngOnDestroy() { 
     this.resizeListener(); 
    } 
} 

但是當我使用其他組件業力測試情況下,這個組件啓動失敗

收到錯誤:

Error: Error in ./TabContainerComponent class TabContainerComponent - inline template:2:2 caused by: this.resizeListener is not a function 
TypeError: this.resizeListener is not a function 
    at InkbarComponent.ngOnDestroy (webpack:///src/components/basic-components/ink-bar/inkbar.component.ts:9:4753 <- src/tests.entry.ts:117472:4760) 
    at Wrapper_InkbarComponent.ngOnDestroy (/WebBasicModule/InkbarComponent/wrapper.ngfactory.js:13:16) 
    at CompiledTemplate.proxyViewClass.View_TabContainerComponent0.destroyInternal (/DynamicTestModule/TabContainerComponent/component.ngfactory.js:342:29) 
    at CompiledTemplate.proxyViewClass.AppView.destroy (webpack:///~/@angular/core/bundles/core.umd.js:12362:0 <- src/tests.entry.ts:23410:19) 
    at CompiledTemplate.proxyViewClass.DebugAppView.destroy (webpack:///~/@angular/core/bundles/core.umd.js:12717:0 <- src/tests.entry.ts:23765:43) 
    at CompiledTemplate.proxyViewClass.View_TabContainerComponent_Host0.destroyInternal (/DynamicTestModule/TabContainerComponent/host.ngfactory.js:43:19) 
    at CompiledTemplate.proxyViewClass.AppView.destroy (webpack:///~/@angular/core/bundles/core.umd.js:12362:0 <- src/tests.entry.ts:23410:19) 
    at CompiledTemplate.proxyViewClass.DebugAppView.destroy (webpack:///~/@angular/core/bundles/core.umd.js:12717:0 <- src/tests.entry.ts:23765:43) 
    at CompiledTemplate.proxyViewClass.AppView.detachAndDestroy (webpack:///~/@angular/core/bundles/core.umd.js:12346:0 <- src/tests.entry.ts:23394:19) 
    at ComponentRef_.destroy (webpack:///~/@angular/core/bundles/core.umd.js:7646:55 <- src/tests.entry.ts:18694:75) 

無法獲得resizeListener作爲函數ngOnDestroy

由於這我得到很多的錯誤

回答

0

您可以檢查resizeListener屬性定義你怎麼稱呼它之前

,或者您可以使用

@HostListener('window:resize', ['$event']) 
onWindowResize(event) { 
    if (this.currentElement) { 
     this.switchPostion(this.currentElement); 
    } 
} 
相關問題