我调用四个不同的REST端点,我想将所有这些结果合并到单个JSON中,并显示在角Mat表中。我试图推入一个数组,但它创建了嵌套的JSON对象,而没有在表中显示任何内容。请在下面找到我的代码:
post.component.ts
import { Component, OnInit, ViewChild, ChangeDetectorRef } from '@angular/core';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort, Sort } from '@angular/material/sort';
import { LiveAnnouncer } from '@angular/cdk/a11y';
import { PostService } from './post-service';
import { Post } from './post';
export class PostComponent implements OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
post: Post[];
dataSource;
columnsToDisplay = ['postId', 'id', 'name', 'email', 'body'];
my_array = [];
/**
* Constructor
*/
constructor(
private postService: PostService,
private _liveAnnouncer: LiveAnnouncer,
private cdr: ChangeDetectorRef) { }
listPost() {
this.onGetPost('post-1');
this.onGetPost('post-2');
this.onGetPost('post-3');
this.onGetPost('post-4');
}
onGetPost(postId): void {
this.postService.getPost(postId).subscribe(
(response) => {
console.log(response);
this.my_array.push(response);
this.post = this.my_array;
this.dataSource = new MatTableDataSource(this.post);
this.cdr.detectChanges();
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
},
(error: any) => {
console.log('entering into error block')
console.log(error)
this.dataSource = new MatTableDataSource();
this.cdr.detectChanges();
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
},
() => console.log('Done getting all post')
);
}
filterData($event: any) {
this.dataSource.filter = $event.target.value;
}
announceSortChange(sortState: Sort) {
if (sortState.direction) {
this._liveAnnouncer.announce(`Sorted ${sortState.direction}ending`);
} else {
this._liveAnnouncer.announce('Sorting cleared');
}
}
/**
* On init
*/
ngOnInit(): void {
this.listPost();
}
}post-service.ts
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { Post } from "./post";
@Injectable({ providedIn: 'root' })
export class PostService {
constructor(private http: HttpClient) { }
endpoint: any;
getPost(id): Observable<Post[]> {
console.log(id);
switch (id) {
case 'post-1':
this.endpoint = 'https://jsonplaceholder.typicode.com/posts/1/comments';
break;
case 'post-2':
this.endpoint = 'https://jsonplaceholder.typicode.com/posts/2/comments';
break;
case 'post-3':
this.endpoint = 'https://jsonplaceholder.typicode.com/posts/3/comments';
break;
case 'post-4':
this.endpoint = 'https://jsonplaceholder.typicode.com/posts/4/comments';
break;
}
return this.http.get<Post[]>(this.endpoint);
}
}你能帮我解决这个问题吗?感谢你在这方面的支持。谢谢!
发布于 2022-04-24 05:33:38
您可以使用RxJ,使用管道和switchMap,然后在每个开关映射中尝试连接API的结果
另一种方法是使用forkJoin,首先将onGetPost的结果更改为可观察的,然后编写如下代码:
let fk = [];
fk.push(onGetPost('x'));
fk.push(onGetPost('y'));
fk.push(onGetPost('z'));在那之后
forkJoin(fk).subscribe(a=> { code to concat result})forkJoin的级联结果
使用叉连接,您可以动态地获得不同id的onGetPost
https://stackoverflow.com/questions/71969070
复制相似问题