在我的 Angular 2 应用程序中,我有如下后端服务。
getUserInterests() {
return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json());
}
调用此服务后,我想在前一个服务成功后调用另一项服务。
第二次服务
let params: URLSearchParams = new URLSearchParams();
params.set('access_token', localStorage.getItem('access_token'));
return this.http.get('http://localhost:8080/user/selections', { search: params }).map((res: Response) => res.json());
这两个服务分别返回两个 JSON 数组。然后我需要使用这两个数组进行一些登录。
EDITED
服务.ts
getUserInterests() {
return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json());
}
getSavedSelections() {
let params: URLSearchParams = new URLSearchParams();
params.set('access_token', localStorage.getItem('access_token'));
return this.http.get('http://localhost:8080/interest/user/selections', { search: params }).map((res: Response) => res.json());
}
getSelectionList() {
var obs = this.getUserInterests().flatMap(
(interests) => {
return Observable.forkJoin([
Observable.of(interests),
this.getSavedSelections()
]);
}
);
return obs;
}
然后我在其他 ts 文件中使用以下内容来调用该服务。
export class InterestsComponent {
private interests;
private saved_interests;
constructor(private dataService: DataService) {
this.dataService.getSelectionList().subscribe(
(result) => {
var interests = result[0];
var selections = result[1];
}
);
}
}
但这在控制台日志上给出了以下错误。
ORIGINAL EXCEPTION: TypeError: this.dataService.getSelectionList is not a function
任何建议表示赞赏。