I'm trying to make a helper to get some URL param for me, using ActivatedRoute.
The normal way, injecting ActivatedRoute on component, is working fine, like this:
constructor(
private route: ActivatedRoute,
private api: MyApiService
) { }
ngOnInit(): void {
this.api.getData(this.route.snapshot.paramMap.get("id") || "").subscribe((data: any) => {
console.log(data);
}, (error: any) => {
console.error(error);
})
}
Now I want to create a helper file with some getIdFromUrl() function that returns the ID string or an empty string, this way:
import { ActivatedRoute } from "@angular/router";
export function getIdFromUrl(): string
{
var route = new ActivatedRoute();
var id = route.snapshot.paramMap.get("id");
return id !== null ? id : "";
}
But can't make this to work and can't figure out why.
Can someone help me with this?
Thanks!