How do I get id from url for Angular Resolver component

Viewed 7837

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.

3 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

You can get the id in the resolver with the snippet below,

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
: Observable<any> | Promise<any> | any {
    let id = route.params['id'];
    return this.service.get(id);
}

Try this,

 constructor(private route: ActivatedRoute) {
    route.params.subscribe(
     (params) => {
       console.log("params.get('id')", params['id']);
     });
  }
Related