简体   繁体   English

从服务获取数据时,Angular 2 rxjs 订阅不是功能

[英]Angular 2 rxjs subscribe is not a function when getting data from service

I'm new to angular and I know this is a basic but I'm having problems passing the _newArray to my child component using a shared service我是 angular 新手,我知道这是一个基本问题,但是我在使用共享服务将_newArray传递给我的子组件时遇到了问题

Service服务

getJson(): Observable < any > {
  return forkJoin(this.getQuestions(), this.getData()).pipe(
    tap(data => {
      this.MergeData(this._questionsArray, this._dataArray);
    })
  );
}

MergeData(question, data): any[]{
  let _newArray: any[] = [];
  _newArray.push({
    test1: "test1",
    test2: "test2"
  });
  return _newArray
}

Component成分

export class CheckComponent implements OnInit {

  _finalArr: any[];

  constructor(private apiService: ApiService) { }

  ngOnInit(): void {
    this.apiService.getJson()
      .subscribe(data => {
        this._finalArr = data;
      });

  }
}

getJson is currently returning a Subscription, it must return an Observable. getJson当前正在返回一个订阅,它必须返回一个 Observable。 Try:尝试:

getJson(): Observable {

    return forkJoin(this.getQuestions(), this.getData()).pipe(
      tap(data => {
          this.MergeData(this._questionsArray, this._dataArray);
      })
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM