处理从另一个方法(http)提取的数据集时出现问题



我在使用Angular http请求时遇到了这个问题。请查看以下代码片段。

http服务.ts

import {Injectable} from '@angular/core';
import {environment} from '../../../../environments/environment';
import {HttpClient, HttpHeaders} from '@angular/common/http';
const BASE_URL = environment.API.MAIN.PATH;
const WAR = environment.API.MAIN.WAR;
@Injectable({
providedIn: 'root'
})
export class HttpService {
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
constructor(private http: HttpClient) {
}
getITById(id) {
return this.http.get(`${BASE_URL}/${WAR}/events/find?id=${id}`);
}
}

应用程序组件.ts

import {Component, Input, OnInit} from '@angular/core';
import {HttpService} from '../../services/http-service.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private httpService: HttpService) {
}
ngOnInit() {
}
processPart(idToPass) {
// get the loaded value from requesPart(idToPass); function and process that data set in this function.
let data = requestPart(idToPass);
}
requestPart(id) {
// (1)
return this.httpService.getITById(id)
.subscribe((data: any) => {
console.log(data)
}, error => {
console.error(error);
}
);
}
}

这就是问题所在。我知道我在(1(中所做的实现是错误的(这将返回subscriber对象(。我想知道如何从requestPart(id);方法中获取数据集,以便在processPart(idToPass);方法中进行处理。

项目结构无法更改,http服务也无法导入组件(正如一些现有解决方案所建议的那样(

如果有人能指出我在这里遗漏了什么,我将不胜感激。

提前谢谢。

试试这个:

requestPart(id) {
// (1)
this.httpService.getITById(id)
.subscribe((data: any) => {
console.log(data);
if(data)
{
return data;
}
}, error => {
console.error(error);
}
);
}

最新更新