第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 2:兩個后端服務調(diào)用第一個服務成功

Angular 2:兩個后端服務調(diào)用第一個服務成功

楊__羊羊 2019-09-03 17:17:57
在我的Angular 2應用程序中,我有后端服務,如下所示。getUserInterests() {    return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json());}在調(diào)用此服務后,我想在上一個服務成功之前調(diào)用另一個服務。第二次服務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數(shù)組。然后我需要用這兩個數(shù)組進行一些登錄。EDITEDservice.tsgetUserInterests() {    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文件中使用以下來調(diào)用該服務。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];        }    );  }}但這會在控制臺日志中出現(xiàn)以下錯誤。ORIGINAL EXCEPTION: TypeError: this.dataService.getSelectionList is not a function任何建議表示贊賞。
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經(jīng)驗 獲得超7個贊

您需要利用flatMap運算符在上一個請求完成后調(diào)用一個請求:


this.service.getUserInterests().flatMap(

  (interests) => {

    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());

  }

);

訂閱此數(shù)據(jù)流時,您只會收到上一個請求的結(jié)果。


您可以使用該Observable.forkJoin方法返回兩者。這是一個示例:


var obs = this.service.getUserInterests().flatMap(

  (interests) => {

    return Observable.forkJoin([

      Observable.of(interests),

      this.service.getUserSelections()

    ]);

  }

);


obs.subscribe(

  (result) => {

    var interests = result[0];

    var selections = result[1];

  }

);


查看完整回答
反對 回復 2019-09-03
  • 1 回答
  • 0 關注
  • 585 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號