我是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;
});
}
}
答案 0 :(得分:2)
getJson
当前正在返回订阅,它必须返回一个Observable。试试:
getJson(): Observable {
return forkJoin(this.getQuestions(), this.getData()).pipe(
tap(data => {
this.MergeData(this._questionsArray, this._dataArray);
})
);
}