如何解决如何获得有关完成的递归http调用的通知?
我正在尝试从服务器递归获取对象。一个对象可以具有多个作为子对象的对象,并且不知道有多少个子对象以及对象嵌套的深度。 我有一个从服务器获取数据并使用它创建打字稿对象并将子级添加到相应父级的函数。我的问题是,我要订阅函数中的结果,所以我不知道何时完成从服务器获取对象的工作。我必须在哪里订阅我的结果?当我不订阅获取功能时,如何仍然可以从数据创建Typescript对象? 这是我目前拥有的:
fetchDataFromServer(token: HttpHeaders,url: string,parent: OwnObject) {
let current: OwnObject;
return this.http //subscribe should be done somwhere else but how can I get notified about the subscribe only if the recursion is completely finished
.get<any>(url,{ headers: token }).subscribe(result => {
if (Array.isArray(result)) {
for (const resultChild of result) {
if (!this.isOfTypeToShow(resultChild)) continue;
else {
current = new OwnObject(resultChild.name,resultChild.type,resultChild.href,resultChild.id,[]);
parent.childObject.push(current);
if (!resultChild.type.endsWith('type that has no children')) { //this type cannot have children so no further search is necessary
const urlOfChildren = this.createURLStringOfChildren(resultChild.relations,current);
if (urlOfChildren !== '') {
return this.fetchDataFromServer(token,urlOfChildren,current);
}
}
}
}
} else {
if (this.isOfTypeToShow(result)) {
current = new OwnObject(result.name,result.type,result.href,result.id,[]);
parent !== null ? parent.childObject.push(current) : this.rootObject.next(current);
if (!result.type.endsWith('type that has no children')) {
const urlOfChildren = this.createURLStringOfChildren(result.relations,current);
if (urlOfChildren !== '') {
return this.fetchDataFromServer(token,current);
}
}
}
}
}));
}
方法createURLStringOfChildren只是根据当前元素的子代创建一个url字符串。通过此字符串,我从服务器获取了子代。 如果元素的类型在上下文中很有趣(某些类型可以忽略),则isOfTypeToShow方法仅返回true。
我还尝试根据this示例使用expand,但是它并没有真正帮助我解决问题。我对Typescript / Javascript中的请求调用的了解有限。 因此,基本上,我只需要知道如何在不订阅该函数的情况下进行该函数的操作,以及在递归http调用完全完成后如何获得通知。 首先从另一个组件调用该函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。