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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Angular2中鏈接Http調(diào)用?

如何在Angular2中鏈接Http調(diào)用?

胡子哥哥 2019-08-19 14:20:04
如何在Angular2中鏈接Http調(diào)用?我是Angular2和Http Observable的新手。我有一個(gè)調(diào)用Http服務(wù)并返回Observable的組件。比我訂閱Observable,它工作正?!,F(xiàn)在,我希望在該組件中,在調(diào)用第一個(gè)Http服務(wù)之后,如果調(diào)用成功,則調(diào)用其他Http服務(wù)并返回該Observable。因此,如果第一次調(diào)用不成功,則組件返回Observable,而它返回Observable的第二次調(diào)用。所以,問題是,鏈接Http調(diào)用的最佳方法是什么?有沒有優(yōu)雅的方式,比如monads?
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

使用rxjs來完成這項(xiàng)工作是一個(gè)非常好的解決方案。它易于閱讀嗎?我不知道。


另一種方法是執(zhí)行此操作并且更具可讀性(在我看來)是使用await / async。


示例:


async getContrat(){

    //get the customer

    const customer = await this.http.get('./customer.json').toPromise();


    //get the contract from url

    const contract = await this.http.get(customer.contractUrl).toPromise();


    return contract; // you can return what you want here

}

然后叫它:)


this.myService.getContrat().then( (contract) => {

  // do what you want

});

或者在異步功能中


const contract = await this.myService.getContrat();

您還可以使用try / catch來管理錯(cuò)誤:


let customer;

try {

  customer = await this.http.get('./customer.json').toPromise();

}catch(err){

   console.log('Something went wrong will trying to get customer');

   throw err; // propagate the error

   //customer = {};  //it's a possible case

}


查看完整回答
反對(duì) 回復(fù) 2019-08-19
  • 3 回答
  • 0 關(guān)注
  • 767 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)