Testing Angular component with unsubscribe Error during cleanup of component

后端 未结 10 1690
梦毁少年i
梦毁少年i 2021-01-30 10:37

I\'m testing a component which subscribe router params. Every test pass and everything works fine. But if I look in the console, I can see an error:

Error

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 11:19

    For me what fixed this error was inside of my component's ngOnDestroy, I wrapped my store dispatch and my unsubscribe in a try catch.

    ngOnDestroy(): void {
     try {
      this.store.dispatch(new foo.Bar(this.testThing()));
      if(this.fooBarSubscription) {
       this.fooBarSubscription.unsubscribe();
      }
     } catch (error) {
       this.store.dispatch(new foo.Bar(this.testThing()));
      }
    }
    

提交回复
热议问题