代码如下:
import { _HttpClient } from '@delon/theme';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
// tslint:disable-next-line: class-name
export class tools {
private http: _HttpClient;
loadSelect(http, url, params): Observable<String[]> {
const newUrl = url + params;
const select = [];
return this.http.get(newUrl).pipe(
map((res: any) => {
if (res.status === 0) {
for (const x of res.data) {
const obj = { label: '', value: '' };
obj.label = x.title;
obj.value = x.title;
select.push(obj);
}
}
return select;
}),
);
}
}
报错如下:
ERROR TypeError: Cannot read property 'get' of undefined
at tools.loadSelect (tools.ts:12)
this.http为空,也就是_HttpClient为空,检查下'@delon/theme',theme从字面上理解是主题样式的意思,是不是引用有误
return this.http.get(newUrl).pipe(
这里http没有get,http在哪里初始化的。
'@delon/theme'; 你看看get方法和你写的一直吗(参数方面)
你引用的库不对 一般是用 @angular/common/http
然后还需要添加构造方法 在里边注入http
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
// tslint:disable-next-line: class-name
export class tools {
constructor(
private http: HttpClient
) { }
loadSelect(http, url, params): Observable<String[]> {
const newUrl = url + params;
const select = [];
return this.http.get(newUrl).pipe(
map((res: any) => {
if (res.status === 0) {
for (const x of res.data) {
const obj = { label: '', value: '' };
obj.label = x.title;
obj.value = x.title;
select.push(obj);
}
}
return select;
}),
);
}
}