在一个角度5项目中,我想在加载视图之前执行一个解析器 . 问题是我想从解析器返回的数据取决于可观察的数据 . 这是代码:
Resolver:
@Injectable()
export class PlayersResolver implements Resolve<any> {
constructor(private playersSrv: PlayersService) {
this.playersSrv.getStandings();
}
resolve() {
return this.playersSrv.standings$;
}
}
Service used by the resolver:
@Injectable()
export class PlayersService {
standingsSubject = new ReplaySubject<any>();
standings$ = this.standingsSubject.asObservable();
constructor(private http: HttpClient) {}
getStandings(): void {
this.http.get('http://localhost:8080/api/fixtures/standings')
.subscribe((response: any) => {
this.standingsSubject.next(response);
});
}
}
尝试导航到视图时使用此代码显示任何内容 . 有关如何实现解决方案以获取组件中的数据的任何想法?
谢谢
1 回答
1.从服务功能返回可观察性 . 2.从服务中删除订阅 . 3.只需调用service方法中的service函数,返回对返回observable的服务函数inturn的调用
4.Resolve guard自动订阅服务调用,下一个路由仅在其解析时加载