【发布时间】:2018-06-05 05:59:04
【问题描述】:
在 Angular 5 项目中,我想在加载视图之前执行解析器。问题是我想从解析器返回的数据取决于一个可观察的。这是代码:
解析器:
@Injectable()
export class PlayersResolver implements Resolve<any> {
constructor(private playersSrv: PlayersService) {
this.playersSrv.getStandings();
}
resolve() {
return this.playersSrv.standings$;
}
}
解析器使用的服务:
@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);
});
}
}
当试图导航到视图时,使用此代码会显示任何内容。关于如何实现解析以获取组件中的数据的任何想法?
谢谢
【问题讨论】:
-
相信我;您不想在服务内部订阅。
-
嗨 Jota.Toledo,为什么不呢?
标签: angular observable angular-services resolver