We have an Angular 2 app using RouterModule. We wrote a simple service that looks like this:
//imports...
@Injectable()
export class ActionService {
actionClick: EventEmitter<any> = new EventEmitter();
click() {
this.actionClick.emit();
}
getActionEmitter() {
return this.actionClick;
}
}
We have a component called 'BaseStepComponent', which uses the ActionService:
//imports...
export class BaseStepComponent{
subscription;
constructor (protected actionService: ActionService){
this.subscription = this.actionService.getActionEmitter()
.subscribe(() => this.actionClicked())
}
actionClicked() {
console.log("Action Clicked!");
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
The BaseStepComponent is a base class for other components, FirstStepComp and SecondStepComp. Our problem is that each time we navigate between those two components, another observer is added to the actionService, but the unsubscribe doesn't seem to work. Assuming we call ActionService.click() after each navigation (including the first), we expect to see one log message every time. But after the second navigation we see two message, three messages after the 3rd nav, and so on. We tried to call
super.ngOnDestroy()
from the children's ngOnDestroy, but it didn't work. What are we missing?
Thanks!
Update - our Routing
Here is how we configured our routing: we have a component called ProgressComponent:
{path: 'progressComp/:step', component: ProgressComponent},
//other routes...
The HTML template of ProgressComponent:
<FirstStepComp *ngIf="curStep == 1">...</FirstStepComp>
<SecondStepComp *ngIf="curStep == 2">...</SecondStepComp>
To advance a step, ProgressComponent.ts navigates to itself with the new step number:
advance() {
this.router.navigate([`../${this.curStep + 1}`], { relativeTo: this.route });
}
Just to clarify - we have other components that uses the ActionService, and the unsubscribe() works normally there.