我正在整理和优化/添加别人的代码。功能已经存在并且可以正常工作,而性能却没有。
这里是构造函数:
constructor(
private globalHelper: GlobalHelper,
private authenticationService: AuthenticationService,
private _sanitizer: DomSanitizer,
private route: ActivatedRoute,
private http: Http,
private authService: AuthenticationService
) {
this.showLoader = false;
this.showNoChampionsAlert = false;
this.disabledFinder = true;
this.queryString = require('querystring');
this.getDivisions();
}
“ require”在编译前后给我以下错误(尽管编译成功并且函数和API都正常工作):
错误 src / pages / collab / champions / finder-page / champions-finder.ts(49,24): 错误TS2591:找不到名称'require'。您需要安装类型吗 节点的定义?尝试
npm i @types/node
,然后将node
添加到 tsconfig中的“类型”字段。
查询字符串仅使用一次:
this.http
.get(
App._URL +
'ha/api/hi/at?' + this.queryString.stringify(data),
...
字符串化输出符合url的语法。
在this stackoverflow answer上介绍了如何在Http调用中传递数据之后,我假设使用5+(我使用angular8)自动进行字符串化。
但是当我将代码更改为此:
this.http
.get(
App._URL +
'ha/api/hi/at?' + data,
...
在浏览器控制台中,我看到该调用具有[Object] [Object]而不是所需的字符串。所以当然会失败。
我很确定可以将仅用于此一件事的外部库完全从项目中删除。
如果没有,我想知道如何“正确”声明它
答案 0 :(得分:2)
如果您想以成角度的角度使用require
,可以这样操作:
import * as queryString from 'queryString';
现在,您无需使用this
就可以使用它。
只需写queryString.stringify(data)
。