簡體   English   中英

Angular 12 http 請求返回未定義的參數

[英]Angular 12 http request returning undefined parameters

In my service.ts file, I am calling a method that makes an http request to the following API: users/1111/state/2222 Where 1111 is the userId and 2222 is the stateId.

service.ts 方法調用

public getParams(userId: number, stateId: number): Observable<MyModel> {
   return this.get(`users%2F${userId}final-rosters%2F${stateId}`)
     .pipe(map(response => response.body));
 }

在我的 component.ts 文件中。 我像這樣調用方法:

public getUser(
    userId: number,
    stateId: number,
  ): void {
    this.myService.getParams(userId, stateId)
      .subscribe(result => {
        this.myModel.next(result);
        console.log('GET DATA ', result);
      });
  }

在我的檢查工具中,我得到以下信息: http : //10.1.9.141 : 9999/api/elections/undefined/final-rosters/undefined

我在將實際 url 路徑轉換為采用 2 個參數的路徑時遇到了一些麻煩。

嘗試正確定義您的服務 URL:

public getParams(userId: number, stateId: number): Observable<MyModel> {

   const configUrl = 'users/${userId}/final-rosters/${stateId}'
   return this.get<MyModel>(configUrl), { observe: 'response' });

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM