【问题标题】:Angular HttpClient not executing with postAngular HttpClient 不与 post 一起执行
【发布时间】:2021-01-11 07:16:15
【问题描述】:

我有一个带有 Angular 应用程序的服务类,如下所示:

@Injectable()
export class AbstractStore {
  protected url: string;

  constructor(protected http: HttpClient) {}

  read(url: string): Observable<any> {
    return this.http.get<any>(URL + url).pipe(
      ...
    );
  }

  loadRecords(): Observable<Record[]> {
    return this.read(this.url);
  }

  loadRecord(id: string): Observable<Record> {
    return this.read(`${this.url}/${id}`);
  }

  saveRecord(record: Record): Observable<Record> {
    // console statement executes
    console.log("Saving record..................", URL + this.url, record);

    // THIS NOT EXECUTE
    return this.http.post<any>(URL + this.url, record).pipe(
      ...
    );
  }

  handleError(error) {
    console.log(error.message || error);
  }
}

当调用读取 (http.get) 方法时,它会正确执行,并且我在“网络”选项卡中看到流量。 当来到saveRecord() 方法时 - 它执行到控制台语句。我没有在“网络”选项卡中看到执行。

我不确定我做错了什么?

【问题讨论】:

    标签: angular httpclient


    【解决方案1】:

    您必须使用subscribetoPromise

    return this.http.post<any>(URL + this.url, record)
      .pipe(
        ...
      ).toPromise();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多