This is how to go about it
Here is an example code on how to go about it
In component.ts
import { Router, ActivatedRoute } from '@angular/router';
export class ClassName implements OnInit {
rootUrl: string;
id: number;
currentRoute: string;
constructor(public router: Router, private _activatedRoute: ActivatedRoute) { }
ngOnInit(): void {
this.currentRoute = router.url;
this.rootUrl = this.router.url.split('/')[1]; // gets the base folder e.g shop in "localhost:4200/shop"
this._activatedRoute.paramMap.subscribe(
params=>{
this.id = +params.get('id'); // grabs the query param "id". the "+" converts it to integer
}
);
}
}
assume your url looks like "localhost:4200/shop/1234"
and you want to show a component based on the url, you have to do the following:
in your component.ts, create public string variable like "currentRoute"
in the constructor, create a Router instance and an ActivatedRoute instance that will hold the url properties. Use the ActivatedRoute instance to get the query parameters from the url and assign it to the variables as demonstrated in my sample code.
then in the component.html file, add ngIf condition to the tag to determine whether to show the component or not
<app-product-header *ngIf="id > 0"></app-product-header>