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