正确的外部库的“2019”构造函数声明/实例化语法
correct "2019" constructor declaration / instantiation syntax for external libraries
我正在取 optimizing/linting 别人的代码。功能在那里并且可以正常工作,只是性能不行...
这是构造函数:
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 return 都有效):
ERROR in
src/pages/collab/champions/finder-page/champions-finder.ts(49,24):
error TS2591: Cannot find name 'require'. Do you need to install type
definitions for node? Try npm i @types/node
and then add node
to
the types field in your tsconfig.
查询字符串只使用一次 :
this.http
.get(
App._URL +
'ha/api/hi/at?' + this.queryString.stringify(data),
...
字符串化输出符合 url 的语法。
跟进 关于如何在 Http 调用中传递数据,我假设 angular 5+(我正在使用 angular8)字符串化是自动的。
但是当我将代码更改为:
this.http
.get(
App._URL +
'ha/api/hi/at?' + data,
...
在浏览器控制台中,我看到调用有 [Object][Object] 而不是所需的字符串。所以当然失败了。
我很确定这个仅用于这一件事的外部库可以从项目中完全删除。
如果没有,我想知道如何申报"correctly"
如果你想在angular中使用require
,你可以这样做:
import * as queryString from 'queryString';
现在您可以在没有 this
的情况下使用它。
只需写 queryString.stringify(data)
.
我正在取 optimizing/linting 别人的代码。功能在那里并且可以正常工作,只是性能不行...
这是构造函数:
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 return 都有效):
ERROR in src/pages/collab/champions/finder-page/champions-finder.ts(49,24): error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try
npm i @types/node
and then addnode
to the types field in your tsconfig.
查询字符串只使用一次 :
this.http
.get(
App._URL +
'ha/api/hi/at?' + this.queryString.stringify(data),
...
字符串化输出符合 url 的语法。
跟进
但是当我将代码更改为:
this.http
.get(
App._URL +
'ha/api/hi/at?' + data,
...
在浏览器控制台中,我看到调用有 [Object][Object] 而不是所需的字符串。所以当然失败了。
我很确定这个仅用于这一件事的外部库可以从项目中完全删除。
如果没有,我想知道如何申报"correctly"
如果你想在angular中使用require
,你可以这样做:
import * as queryString from 'queryString';
现在您可以在没有 this
的情况下使用它。
只需写 queryString.stringify(data)
.