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

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

在 Angular 7 中填充對(duì)象模型

在 Angular 7 中填充對(duì)象模型

Qyouu 2021-12-12 10:01:37
我這幾天一直有問(wèn)題。我正在嘗試使用對(duì) JSON API 的查詢結(jié)果填充對(duì)象。我需要填寫(xiě)一個(gè)模型,因?yàn)橥ㄟ^(guò)它我需要釘一個(gè)鍵來(lái)在另一個(gè) api 中進(jìn)行另一個(gè)查詢并在屏幕上返回?cái)?shù)據(jù)。但到目前為止我能得到的都是未定義的為了更好地理解我需要填充生成對(duì)象,以便通過(guò)它我可以填充另一個(gè)對(duì)象的數(shù)據(jù)并獲取一個(gè) url 來(lái)查詢另一個(gè)端點(diǎn) api 并從屏幕返回其他數(shù)據(jù)。export class PokeApp implements OnInit  {   gen1: Generation[];   gen2: Generation[];    generation : Generation;  constructor(private http: HttpClient, private gService:GenerationService) {  }  ngOnInit(){   this.getGeneration1();   this.getGeneration2();   // Only for test, this is not the data ho i need - but this object generation returns null, i do not now how.   this.gService.getPokemon_Species(this.generation.name);  }// this request return a gen1 object to my screen, but a need this object in JS code// to do another query.  getGeneration1(): void{    this.gService.getGeneration1().subscribe(gen =>{    this.gen1 = gen    this.generation = gen[0];  });  }  getGeneration2(): void{    this.gService.getGeneration2().subscribe(gen => this.gen2 = gen);    console.log("Still Returned Undefined___>>>>" + this.generation); }// this do the request to a Poke APIexport class GenerationService {  private GetGenerationURL1 = "https://pokeapi.co/api/v2/generation/1";  private GetGenerationURL2 = "https://pokeapi.co/api/v2/generation/2";  httpOptions = { headers: new HttpHeaders({ "Content-Type": "application/json" }) };  constructor(private http: HttpClient) { }  getGeneration1(): Observable<Generation[]> {    return this.http.get<Generation[]>(this.GetGenerationURL1)      .pipe(        tap(_ => console.log('fetched generations')),        catchError(this.handleError<Generation[]>('getGeneration1', []))      );    // Subscribe to begin listening for async result  }  getGeneration2(): Observable<Generation[]> {    return this.http.get<Generation[]>(this.GetGenerationURL2)    .pipe(      tap(_ => console.log('fetched generations')),      catchError(this.handleError<Generation[]>('getGeneration2', []))    );  }
查看完整描述

1 回答

?
德瑪西亞99

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

更新


所以問(wèn)題實(shí)際上出在打字上。您不需要[]在Generation任何地方之后添加。因?yàn)闆](méi)有任何地方可以讓 API 用世代數(shù)組來(lái)響應(yīng)。


因此,[]從getGeneration1服務(wù)中 HTTP 的類(lèi)型化響應(yīng)的返回類(lèi)型和類(lèi)型中刪除。


請(qǐng)注意,Typescript 中的類(lèi)型僅用于編譯時(shí),它不會(huì)影響運(yùn)行時(shí)中的任何內(nèi)容,只是為了確保您使用正確的引用并在運(yùn)行前檢測(cè)錯(cuò)誤。


我在這里添加 getGeneration 函數(shù):


getGeneration1(): Observable<Generation> {

  return this.http.get<Generation>(this.GetGenerationURL1)

    .pipe(

      tap(_ => console.log('fetched generations')),

      catchError(this.handleError<Generation>('getGeneration1', []))

    );

}


getGeneration2(): Observable<Generation> {

  return this.http.get<Generation>(this.GetGenerationURL2)

  .pipe(

    tap(_ => console.log('fetched generations')),

    catchError(this.handleError<Generation>('getGeneration2', []))

  );

}

在組件中,您需要像這樣重構(gòu)它:


export class PokeApp implements OnInit  {


gen1: Generation;

gen2: Generation;

generation : Generation;


constructor(private http: HttpClient, private gService:GenerationService) {


}


ngOnInit(){

  this.getGeneration1();

  this.getGeneration2();

  this.gService.getPokemon_Species(this.generation.name);

}


getGeneration1(): void{

    this.gService.getGeneration1().subscribe(gen =>{

      this.gen1 = gen

      this.generation = gen;

  });


}


getGeneration2(): void{

    this.gService.getGeneration2().subscribe(gen => this.gen2 = gen);

}

這是為了防止您仍然需要組件中的代碼在不鏈接我在舊答案中提供的響應(yīng)的情況下工作,但我建議重構(gòu)您的代碼,如下所示:


getGenerations() {

  this.gService.getGeneration1()

    .pipe(mergeMap(gen => {

      this.generation = gen;

      return this.gService.getGeneration2();

    }))

    .pipe(mergeMap(gen => {

      return this.gService.getPokemon_Species(this.generation.name);

    }))

    .subscribe(response => console.log(response));

}

舊答案


您很需要使用mergeMap。它應(yīng)該是這樣的:


getGenerations() {

  this.gService.getGeneration1()

    .pipe(mergeMap(gen => {

      this.gen1 = gen;

      this.generation = gen[0];

      return this.gService.getGeneration2();

    }))

    .pipe(mergeMap(gen => {

      this.gen2 = gen;

      return this.gService.getPokemon_Species(this.generation.name);

    }))

    .subscribe(response => console.log(response));

}


查看完整回答
反對(duì) 回復(fù) 2021-12-12
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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