Search code examples
angularangular-routing

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.


Solution

  • 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