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
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.