gpt4 book ai didi

angular - 在订阅方法中构建父子对象

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:12 24 4
gpt4 key购买 nike

我有一个 Angular 组件,它订阅了一个由 Angular 服务返回的可观察对象。

组件:help.component.ts

import { WikiService } from '../../../services/wiki.service';

import { WikiTree } from '../../../interfaces/WikiTree';

export class HelpComponent implements OnInit {

wikiTree$: Observable<WikiTree>
wikiChildTree$: Observable<WikiTree>

public infoCards: Array<Object>;

constructor(private wikiService: WikiService) {}

ngOnInit() {
this.wikiTree$ = this.wikiService.GetWikiHierarchy();
this.wikiTree$.subscribe((data)=>{
const newInfoCards = data.page.results.map(result => (
{
"image": "",
"title": result.title,
"titleLink": "/page/wiki/"+ result.id,
"children": [] /*TODO: Populate this array with a Service call based on result.id*/
}))
this.infoCards = [...this.infoCards,...newInfoCards]
},
(err) => {
console.log(err);
}
);
}

wikiTree$ Observable 具有以下 JSON,已转换为 TypeScript:

{
"page": {
"results": [
{
"id": "123456789",
"type": "page",
"status": "current",
"title": "Start here",
"extensions": {
"position": 0
},
"_links": {
"webui": "/display/MYSPACE/Start+here",
"edit": "/pages/resumedraft.action?draftId=123456789",
"tinyui": "/x/BQD2Mw",
"self": "https://wiki.abc.com/rest/api/content/123456789"
},
"_expandable": {
"container": "/rest/api/space/MYSPACE",
"metadata": "",
"operations": "",
"children": "/rest/api/content/123456789/child",
"restrictions": "/rest/api/content/123456789/restriction/byOperation",
"history": "/rest/api/content/123456789/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/123456789/descendant",
"space": "/rest/api/space/MYSPACE"
}
},
{
"id": "567890123",
"type": "page",
"status": "current",
"title": "FAQ",
"extensions": {
"position": 1
},
"_links": {
"webui": "/display/MYSPACE/FAQ",
"edit": "/pages/resumedraft.action?draftId=567890123",
"tinyui": "/x/HQD2Mw",
"self": "https://wiki.abc.com/rest/api/content/567890123"
},
"_expandable": {
"container": "/rest/api/space/MYSPACE",
"metadata": "",
"operations": "",
"children": "/rest/api/content/567890123/child",
"restrictions": "/rest/api/content/567890123/restriction/byOperation",
"history": "/rest/api/content/567890123/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/567890123/descendant",
"space": "/rest/api/space/MYSPACE"
}
}
],
"start": 0,
"limit": 25,
"size": 2,
"_links": {
"self": "https://wiki.abc.com/rest/api/content/998877665/child/page"
}
},
"_links": {
"base": "https://wiki.abc.com",
"context": "",
"self": "https://wiki.abc.com/rest/api/content/998877665/child"
},
"_expandable": {
"attachment": "/rest/api/content/998877665/child/attachment",
"comment": "/rest/api/content/998877665/child/comment"
}
}

typescript :WikiTree.ts

export interface WikiTree {
page: Page;
_links: Links;
_expandable: Expandable;
}
export interface Page {
results?: (ResultsEntity)[] | null;
start: number;
limit: number;
size: number;
_links: Links1;
}
export interface ResultsEntity {
id: string;
type: string;
status: string;
title: string;
extensions: Extensions;
_links: Links2;
_expandable: Expandable1;
}
export interface Extensions {
position: number;
}
export interface Links2 {
webui: string;
edit: string;
tinyui: string;
self: string;
}
export interface Expandable1 {
container: string;
metadata: string;
operations: string;
children: string;
restrictions: string;
history: string;
ancestors: string;
body: string;
version: string;
descendants: string;
space: string;
}
export interface Links1 {
self: string;
}
export interface Links {
base: string;
context: string;
self: string;
}
export interface Expandable {
attachment: string;
comment: string;
}

我想根据它的父 result.id 调用服务来填充 children 数组

例如:调用将是 Wiki 服务中的一个函数,该函数返回一个可观察值。

this.wikiChildTree$ = this.wikiService.GetWikiHierarchy(result.id);

根据 wikiChildTree$ observable 中返回的数据,我想创建一个包含 titlelink 对象的数组。因此,infoCards 对象数组将反射(reflect)一个 JSON 对象,如下所示:

[{
"image": "",
"title": "Start here",
"titleLink": "/page/wiki/123456789",
"children": [{
"title": "Storm",
"link": "/page/wiki/660431"
},
{
"title": "Weather",
"link": "/page/wiki/660432"
}]
},
{
"image": "",
"title": "FAQ",
"titleLink": "/page/wiki/567890123",
"children": [{
"title": "Cloud",
"link": "/page/wiki/450433"
},
{
"title": "Sunshine",
"link": "/page/wiki/120432"
}]
}
]

这就像进行父子异步调用,以获取子数据。

我已经阅读了 forkjoin 和 mergemap,但不确定这里的实现。我怎么做?

最佳答案

所以基本上你应该能够这样做:

ngOnInit() {
this.wikiTree$ = this.wikiService.GetWikiHierarchy().pipe(
switchMap(data =>
forkJoin(data.page.results.map(result => this.wikiService.GetWikiHierarchy(result.id))).pipe(
map(children => ({data, children}))
)
)
).subscribe(({data, children}) => {
const newInfoCards = data.page.results.map((result, i) => (
{
"image": "",
"title": result.title,
"titleLink": "/page/wiki/"+ result.id,
"children": children[i]
}))
this.infoCards = [...this.infoCards,...newInfoCards]
},
(err) => {
console.log(err);
}
);
}

解释:

  1. 您加载this.wikiService.GetWikiHierarchy()
  2. 您使用 forkJoin 为每个 result 同时加载所有 this.wikiService.GetWikiHierarchy(result.id)
  3. 您将来自两个地方的加载数据映射到对象 {data, children} 其中 data - 来自第一次调用的结果 children - 来自第二个forkJoin(子数组或子数组[][])
  4. 之后,您需要将 data.page.resultschildren(这是数组的数组) 连接起来。希望对您有所帮助。

关于angular - 在订阅方法中构建父子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54511091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com