处理从另一方法(http)提取的数据集时遇到的问题

问题描述 投票:1回答:1

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

http-service.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}`);
  }
}

app-component.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)中进行的实现是错误的(这将返回订户对象)。我想知道如何获取从requestPart(id);方法中提取的数据集以在processPart(idToPass);方法中进行处理。

无法更改项目结构,并且无法将http-service导入组件(如建议的某些现有解决方案)

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

谢谢。

angular http subscribe
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.