亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

當上一個 api 成功完成后,如何對 api 進行新的調用?

當上一個 api 成功完成后,如何對 api 進行新的調用?

慕森卡 2023-09-07 17:02:40
我是 Angular 和 rxjs 的新手,我有以下場景,在該場景中,我需要在成功解析對 api 的調用后進行新的調用,在 Angular / rxjs 的上下文中我不知道該怎么做它handler(): void {  this.serviceNAme    .createDirectory(this.path)    .pipe(      finalize(() => {        this.someProperty = false;      })    )    .subscribe(      (data) => console.log(data),      (error) => console.error(error.message)    );}當上一個 api 成功調用時,重新調用 api 的正確方法是什么?
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

我知道你有 aserviceOne和 a?serviceTwo。并且您想serviceTwo使用從 檢索到的數據進行調用serviceOne。

使用 rxjs?switchMap您可以將一個可觀察值通過管道傳輸到另一個可觀察值中。

? handler(): void {

? ? ? ? this.serviceOne

? ? ? ? ? ? .createDirectory(this.path)

? ? ? ? ? ? .pipe(

? ? ? ? ? ? ? ? switchMap(serviceOneResult => {

? ? ? ? ? ? ? ? ? ? // transform data as you wish

? ? ? ? ? ? ? ? ? ? return this.serviceTwo.methodCall(serviceOneResult);

? ? ? ? ? ? ? ? })

? ? ? ? ? ? )

? ? ? ? ? ? .subscribe({

? ? ? ? ? ? ? ? next: serviceTwoResult => {

? ? ? ? ? ? ? ? ? ? // here we have the data returned by serviceTwo

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? error: err => {},

? ? ? ? ? ? });

? ? }

如果您不需要從serviceOne到傳遞數據serviceTwo,但需要它們一起完成,則可以使用 rxjs?forkJoin。

? handler(): void {

? ? ? ? forkJoin([

? ? ? ? ? ? this.serviceOne.createDirectory(this.path),?

? ? ? ? ? ? this.serviceTwo.methodCall()

? ? ? ? ])

? ? ? ? .subscribe({

? ? ? ? ? ? next: ([serviceOneResult, serviceTwoResult]) => {

? ? ? ? ? ? ? ? // here we have data returned by both services

? ? ? ? ? ? },

? ? ? ? ? ? error: err => {},

? ? ? ? });

? ? }


查看完整回答
反對 回復 2023-09-07
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

使用aysncandawait你可以這樣做:


async handler(): void {

  await this.serviceNAme

    .createDirectory(this.path)

    .pipe(

      finalize(() => {

        this.someProperty = false;

      })

    )

    .subscribe(

      (data) => console.log(data),

      (error) => console.error(error.message)

    );


   // Do second api call

}


查看完整回答
反對 回復 2023-09-07
?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

有一些說法可以做到這一點:


場景#1

您的兩個服務 api 調用是獨立的,您只想調用一個,然后調用下一個


 const serviceCall1 = this.serviceName.createDirectory(this.path);

 const serviceCall2 = this.serviceName.createDirectory(this.otherPath);


 concat(serviceCall1 , serviceCall2).subscribe({

   next: console.log,

   error: err => console.error(err.message),

   complete: () => console.log("service call 1&2 complete")

 });

場景#2

您的兩個調用相互依賴,因此您需要第一個調用的結果才能開始第二個調用


 this.serviceName.getDirectoryRoot().pipe(

   switchMap(root => this.serviceName.createDirectoryInRoot(root, this.path))

 ).subscribe({

   next: console.log,

   error: err => console.error(err.message),

   complete: () => console.log("service call 1 used to create service call 2, which is complete")

 });

您將需要方案 # 2,因為這樣做,第一次調用中的錯誤將意味著沒有結果發送到switchMap,并且永遠不會進行第二次調用。


查看完整回答
反對 回復 2023-09-07
  • 3 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號