Ionic 4 resolver service

#1

i use resolver service according to this tutorial of simon grimm
but in this tutorial resolver return a static function; but i have 4 type detail pages and i don’t want create a resolver for each one
i want if going to #1detailPage resolver return Performance related to #1detailPage and when going #2detailPage it’s return Performance related to #2detailPage and that way

resolver service.ts

export class ResolverService implements Resolve<any> {
  constructor( private http: HttpClient) { }

  resolve(route: ActivatedRouteSnapshot){
    let id = route.paramMap.get('id');
    return this.http.get('http://test.com/related#1detailPage')
  }
}

in above code i have 4 different api address (http://test.com/related#1detailPage) for 4 type detailpage

Thanks

#2

Hi @ebeliejinfren,

You can always check which route is being activated by getting the path or appropriate segment from the route parameter, and return with the corresponding HTTP call.

export class ResolverService implements Resolve<any> {
  constructor(private http: HttpClient) {}

  resolve(route: ActivatedRouteSnapshot) {
    // Adapt this to get the URL segment that you want to check from the ActivatedRouteSnapshot
    // https://angular.io/api/router/ActivatedRouteSnapshot
    // https://angular.io/api/router/UrlSegment
    const segment = route.url[0].path;

    switch (segment) {
      case '1detailPage':
        return this.http.get('http://test.com/related#1detailPage');
      case '2detailPage':
        return this.http.get('http://test.com/related#2detailPage');
      default:
        return this.http.get('http://test.com/related');
    }
  }
}

Best,
Rodrigo

1 Like