我正在尝试从API获取项目列表,但出现以下错误:
Type 'TestFields[]' provides no match for the signature '(): TestFields[]'. [2322]
下面是我的代码
export class ProjectOperation extends TestFields {
projectList: TestFields[];
constructor(
private _http: HttpClient,
private _services: DeoService
) {
super();
// this.projectList = new TestFields()
}
getProjects(): TestFields[] {
// below is the line where i am getting error
this._services.getProjects('/api/project/getProject').subscribe(res => {
this.getProjects = res;
}); // this is the area i am getting error
return this.projectList;
}
}
这是我的获取项目代码
public getProjects(api: string): Observable<TestFields[]> {
return this._http.get<TestFields[]>(api);
}
可以从服务返回方法,而不是将方法的结果分配给变量,然后返回它:
export class ProjectOperation extends TestFields {
constructor(
private _http: HttpClient,
private _services: DeoService
) {
super();
}
getProjects(): TestFields[] {
return this._services.getProjects('/api/project/getProject');
}
}