Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular resolver get an Observable

I work on Angular 10 project. I get data from my backEnd with a simple

return this.http
      .get<{ returned: Animal[] }>('animals')
      .pipe(map((res) => res.returned.map((value) => value)));

this is call by my resolver and send through router in my routing module with

resolve: {
        animals: AnimalResolver,
}

I get resolve data with this.route.snapshot.data.animals

I want to have an Obervable<Animal[]> for use AsyncPipe. The type return by the resolver is an Observable<Animal[]> but I get an error invalid Pipe argument, to resolve this error I need to make :

of(this.route.snapshot.data.animals);

Why?

like image 439
Alphablony Avatar asked May 12 '26 16:05

Alphablony


1 Answers

In this case you need to use this.route.data observable where route is ActivatedRoute while ActivatedRouteSnapshot is just "static" data of current route and there are no observables here.

this.animals$ = this.route.data
  .pipe(map(data => data.animals));
like image 105
vadimk7 Avatar answered May 14 '26 07:05

vadimk7