Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get id from url for Angular Resolver component

I'm using a resolver component but I cannot get the id from the url.

Stackblitz - https://stackblitz.com/edit/angular-x4djgz

In the stackblitz if I navigate to supplier/3:

I get params.get('id') null from the resolver and params.get('id') 3 from the end component. How do I get the endpoint id inside the resolver.

route.paramMap.subscribe(
    (params: ParamMap) => {
      console.log("params.get('id')", params.get('id'));
    }
);

This question has been heavily edited as I thought originally this had something to do with being an angular+electron app.

like image 778
Andrew Allen Avatar asked Jun 18 '19 12:06

Andrew Allen


People also ask

How can I get current URL in Angular?

Steps to get current route URL in Angular. Import Router,NavigationEnd from '@angular/router' and inject in the constructor. Subscribe to the NavigationEnd event of the router. Get the current route url by accessing NavigationEnd's url property.


1 Answers

According to the Angular doc, as you can see in example in the guide, the resolve method takes two parameters that are relative to the actual route your are trying to resolve.

Using this in the resolver fixes the problem:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
    console.log("params.get('id')", route.paramMap.get('id'));
    return of('dummy').pipe(delay(50));
  }

https://stackblitz.com/edit/angular-4fkspm

like image 152
bracco23 Avatar answered Oct 20 '22 04:10

bracco23