簡體   English   中英

Angular httpClient不執行網絡請求

[英]Angular httpClient not performing network request

在我的一個Service類中,我嘗試對firebase authentification API執行API請求。 Service類看起來像這樣:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { catchError, tap } from 'rxjs/operators';
import { Observable, of } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private apiKey: string = '[API-Key]';

  private userToken: string;

  login(email: string, password: string): Observable<HttpResponse<string>> {
    console.log('Ich wurde ausgeführt.')
    return this.httpClient.post<string>(`https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=${this.apiKey}`, 
    {email: email, password: Password, returnSecureToken: true}, {observe: 'response'})
    .pipe(
      tap(
        response => {
          console.log(response); 
          this.userToken = response.idToken;
        }, 
        error => {
          console.log(error);
          return error;
        }
      )

    )
  }

  constructor(private httpClient: HttpClient) { }
}

在同一時刻,如果請求成功執行,我想使用httpClient post請求中的響應來將Response提供的令牌值設置為Service類中的private userToken: string變量。

再次結束嚎叫故事:我的問題是這個網絡請求firebase Auth API沒有被觸發,我不知道為什么。 此外,我認為我的代碼處理響應非常好,可以改進。

誰能幫我? 非常感謝你,祝你有個美好的一天!

就像你想要使用它的組件一樣

import { OnInit, Component } from '@angular/core'

export class Component implements OnInit { 

   constructor(private authService: AuthService){}

   ngOnInit() {
      this.authService.login(param1, param2).subscribe(result => {}, err => {});
   }


}

暫無
暫無

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

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