文章内容

2023/5/4 16:44:46,作 者: 黄兵

Angular HttpClient HttpParams() 设置参数

下面是一些删除数据的示例:

public deleteFeatureInPlant(featureId: number, plantId: number): Observable<IPCrawlerModel> {
return this.http.delete<IPCrawlerModel>(`${this.requestSMSReceiveUrl}/product/features`, {
headers: new HttpHeaders().set('Authorization', this.auth.accessToken),
params: new HttpParams().set('featureId', String(featureId)).set('plantId', String(plantId)),
});
}

这里使用了 HttpHeaders() 设置 http header,来传递认证信息。同时使用 HttpParams() 传递需要删除的参数,这里需要注意两个问题:

1、HttpParams() 的多参数,有以下几种写法:

let params = new HttpParams().set('aaa', 'A');    // now it has aaa
params = params.set('bbb', 'B');                  // now it has both

或者是使用链式写法:

const params = new HttpParams()
  .set('one', '1')
  .set('two', '2');

以上就是本文全部内容。


参考资料:

1、Angular 4.3 - HttpClient set params


其它相关推荐:

1、chatGPT 使用随感

2、Typescript for...of循环

3、Typescript空和undefined判断

4、Angular - “has no exported member 'Observable'”

5、Angular HttpClient get 提交方式


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - Angular HttpClient HttpParams() 设置参数

分享到:

发表评论

评论列表