Make use of the new inject function feature from @angular/core which allows you to inject any dependency you would need, for example, a service
service
interface Hero {
name: string;
}
@Injectable()
export class HeroService {
constructor(private http: HttpClient) {}
getHero(id: string): Observable<any> {
return this.http.get(...);
}
}
functional resolver
export const heroResolver: ResolveFn<Hero> =
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
return inject(HeroService).getHero(route.paramMap.get('id')!);
};
Then access the resolver data from whatever component needs it like the following:
component
@Component({template: ''})
export class HeroDetailComponent {
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.activatedRoute.data.subscribe(
({hero}) => {
// do something with your resolved data ...
});
}
}
I recommend reading the official Angular docs, this example was taken from there.