Angular 5 HttpParams 未设置

标签 angular http-parameters

我有非常简单的功能:

createParams(paramsArray, withToken: boolean): HttpParams {
    let params = new HttpParams();
    let currentUser = JSON.parse(localStorage.getItem('currentUser'));
    params.set('access_token', JSON.stringify(currentUser.token));
    return params;
}

当我调试这个时,params 变量不包含任何键或值:

enter image description here

我做错了什么?

最佳答案

试试这个:

let Params = new HttpParams();
Params = Params.append('access_token', JSON.stringify(currentUser.token));

或者

let params = new HttpParams().set('access_token', JSON.stringify(currentUser.token)); 

HttpParams is intended to be immutable. The set and append methods don't modify the existing instance. Instead they return new instances.

关于Angular 5 HttpParams 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50481850/

相关文章:

android - 如何减少 IONIC 2 应用程序的启动时间?

javascript - 如何处理来自封装函数的 Promise

forms - HTTP 参数名称中的下划线与破折号

Lucee 服务器上的 API 响应文件内容为空

angular - 无法在 POST url Angular 传递参数

rest - 使用一长串查询参数设计 RESTful 查询 API

javascript - 未定义数组

angular - Ionic2 Angular2 : I can't seem to get an ng2-chart to appear in my app

javascript - 如何等待在 map 内解决的 promise ?