Angular HttpClient 未执行 post
Angular HttpClient not executing with post
我有一个服务 class,它有一个 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()
方法时 - 它执行到控制台语句。我在“网络”选项卡中没有看到执行。
我不确定我做错了什么?
您必须使用 subscribe
或 toPromise
return this.http.post<any>(URL + this.url, record)
.pipe(
...
).toPromise();
我有一个服务 class,它有一个 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()
方法时 - 它执行到控制台语句。我在“网络”选项卡中没有看到执行。
我不确定我做错了什么?
您必须使用 subscribe
或 toPromise
return this.http.post<any>(URL + this.url, record)
.pipe(
...
).toPromise();