I am trying to detect changes in the url but I am not succeeding, so far I have this:
homeComponent.ts
constructor(private router: Router,private activatedRoute: ActivatedRoute){}
ngOnInit() {
this.activatedRoute.url.subscribe(url => console.log('The URL changed to: ' + url));
this.activatedRoute.queryParams.subscribe(params => console.log('The URL changed to: ' + params["name"]));
this.activatedRoute.paramMap.subscribe((params: ParamMap) => { console.log(params) });
}
homeComponent.html
<app-navbarcust (logout)="logout()"></app-navbarcust>
<div class="container mt-5" [ngClass]='{"pt-5":true}'>
<router-outlet></router-outlet>
</div>
he <app-navbarcust>
is the component of the navigation there are the routerLink they serve well, this I get in the console:
I already changed the route several times and I don't get the reactions, could someone please tell me what I'm doing wrong, another question I'm not unsubscribing in the ondestroy, is this wrong?
You should subscribe in the ngOnInit, or in the constructor, and the function I use to detect route changes is router.events and filtering by NavigationEnd.
And it is not bad that you unsubscribe in the ondestroy, because if you reload the component, it will resubscribe you to the event, and you will have two subscribers, but you will have to subscribe assigning it to a variable to unsubscribe that same object.