0
我有两个观测连接两个观测与flatMap - Angular2
getProfile(nickname: string, server: string): Observable<Profile> {
return this._http.get('http://localhost:3000/summoners?name='+ nickname + '&server=' + server)
.map((res: Response) => res.json())
.map((res: Object) => new Profile().from(res));
}
这种模式
export class Profile {
public id: number;
public accountId: number;
public name: string;
public summonerLevel: number;
,第二个是
export class League {
public name: string;
public tier: string;
public queue: string;
public entries: LeaguePlayer[] = [];
所以,我想加入这两个观察到一个,我知道我需要使用flatMap来做到这一点,但我不知道这是如何做到的。 我试过去做
getProfileWithLeagues(nickname: string, server: string): Observable<Profile> {
return this._http.get('http://localhost:3000/summoners?name='+ nickname + '&server=' + server)
.map(response => response.json().data as Profile)
.flatMap((profiles: Profile) => Observable.forkJoin(profiles.map((summoner: Profile) => {
return this.getLeagues(summoner.id, server)
.map((leagues: League) => {
summoner.leagues.from(leagues);
return summoner;
});
}))
);
}
但这不起作用。我做错了什么,我能做些什么来做我想做的事?感谢所有:)
你是什么意思,它不工作?有关您在调试时看到的内容的情况会很有帮助。 –
@ jonnysai是我要开始的地方。看起来你从不订阅flatmap observable。 forkjoin似乎不必要。 –
这段代码只返回de Profile,与联盟有关,我需要将这两个对象加入另一个对象 – Janba