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

时间:2018-07-17 22:16:48

标签: angular typescript rxjs

我是angular的新手,我知道这是基本知识,但是在使用共享服务将_newArray传递给子组件时遇到了问题

服务:

     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
}

组件:

export class CheckComponent implements OnInit {
  _finalArr: any[];

  constructor(private apiService: ApiService) {
  }

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

  }


}

1 个答案:

答案 0 :(得分:2)

getJson当前正在返回订阅,它必须返回一个Observable。试试:

getJson(): Observable {

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