ObjectUnsubscribedError: object unsubscribed error when I am using ngx-progress in angular 2

前端 未结 4 1012
自闭症患者
自闭症患者 2021-02-04 03:12

I am using ngx-progressbar bar in Angular 2 application. When app loading first it is working fine. second time it is showing error. I referred few article like medium.com for s

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 03:43

    I really liked the way @kbpontius told, so I am also doing the same approach

    Whenever you subscribe, subscribe in the new variable. So after unsubscribe it can be subscribed Examples

    Bad Code:

    this.someEvent.subscribe(() => {
        // DO SOMETHING
    })
    ...
    
    ngOnDestroy() {
         this.someEvent.unsubscribe()
    }
    

    Good Code:

    Declare the event name

    myEventSubscription: any;
    

    //Now it can be use like this

    this.myEventSubscription = this.someEvent.subscribe(() => {
         // DO SOMETHING
    })
    

    ...

    //Now unsubcribe this in last phase of destroying component. You can destroy in other function too if you want to destroy the service in the same component

    ngOnDestroy() {
      this.myEventSubscription.unsubscribe()
    }
    

    Again, you need to unsubscribe from the Subscription object that the .subscribe() method returns when you call it.

提交回复
热议问题