pre*_*vox 1 sorting http httpclient typescript angular
我正在从 Http 转移到 HttpClient,但现在我无法使用 map() 对结果进行排序,因此出现了一些错误。
使用 HttpClient,我得到“对象”类型上不存在属性“排序”。
this.getConcept().subscribe(res => {
res.sort((f, n): number => {
if (f.code < n.code) return -1;
if (f.code > n.code) return 1;
return 0;
});
console.error(res);
this.arrConcept = res;
});
Run Code Online (Sandbox Code Playgroud)
使用 Http 我可以毫无问题地对其进行排序
this.getConcept().map(this.extractData).subscribe(res => {
res.sort((f, n): number => {
if (f.code < n.code) return -1;
if (f.code > n.code) return 1;
return 0;
});
console.error(res);
this.arrConcept = res;
});
Run Code Online (Sandbox Code Playgroud)
res
我只需指定as的类型就可以让它工作[]
this.getConcept().subscribe((res:[]) => {
res.sort((f, n): number => {
if (f.code < n.code) return -1;
if (f.code > n.code) return 1;
return 0;
});
console.error(res);
this.arrConcept = res;
});
Run Code Online (Sandbox Code Playgroud)
然后代码就知道 res 是一个数组,并且数组有一个sort
方法。
如果您为数组定义了接口,那么最好使用它。例如:
(res:IConcept[]) => { ... }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9085 次 |
最近记录: |