In the ngOnDestroy method, I unsubscribe from an observable which I subscribed to once else the code is multiple times executed...
ngOnInit()
{
this.sub = this.route.params.subscribe(params =>
{
this.projectId = +params['id'];
this.projectStore.project = this.projectId;
// load data when the route changes
this._tasksService.gettasks(this.projectId).subscribe(years => { this.tasks = years.map(y => new task(y)) }); // no need to clean up the subscription
});
// load data when the component is initialized
this._tasksService.gettasks(this.projectId).subscribe(years => { this.tasks = years.map(y => new task(y)) }); // no need to clean up the subscription
}
ngOnDestroy()
{
this.sub.unsubscribe();
}
Now I want to put this in a router resolve class but there is no ngOnDestroy - of course - just a NavigationEnd event which I again could subscribe to.
That means I subscribe to a NavigationStart event (which occurs when I leave the route) in order to unsubscribe another subscription which is the route params change subscription HAHAHA...
I guess that's not the way to go, but Google offered nothing.
Anyone knows how to tackle that scenario? Or should route params change subscription really ONLY belong into a component?
constructor(private service: TasksService, private router: Router)
{
this.navigationEnded = this.router.events
.filter(event => event instanceof NavigationStart)
.map(() => this.router.routerState.root)
.subscribe((event) =>
{
this.navigationEnded.unsubscribe();
});
}
UPDATE
When I put this code in the resolve method:
this.route.params.subscribe(params =>
{
// reload data by the new id parameter does not work with params
// I have to use inside here: route.params['id']
});
there is no id in the params array, its just of length 0.
Instead, I have to use route.params['id'] inside the params subscription, but why?