在过去,我会定义一个新的Promise,延迟它,然后在我的函数完成时解决它。
现在我发现自己处于需要从函数返回Observable的情况下,并且我想确保在Angular 5上正确设置它。
测试函数,工作正常:
测试函数,工作正常:
getTreeViewChildren(currNode: any, nodeType: string, nodeTypeEnum: number = 0, nested: boolean = true)
: Observable<any>
{
let treeObs: Observable<any>;
treeObs = new Observable(obs => {
this.siteConfigService.getHostAccessPoints().subscribe(data => {
let hostAccessPoints = JSON.parse(data);
let accPointTree = this.findChildNodes(currNode, hostAccessPoints, nested);
obs.next(accPointTree);
obs.complete();
});
});
return treeObs;
}
我从我的NG组件成功订阅:
this.configService.getTreeViewChildren(this.selectedTreeViewItem, type, nodetype, true).subscribe(tree =>{
let theTree = tree;
});
然而实际功能具有更多的检索逻辑。根据我的测试函数,设置 Observable
的正确方法是什么?是否完全相同的模式,只是将所有代码包装在 treeObs = new Observable(..)
中?
getTreeViewChildNodes(currNode: any, nodeType: string, nodeTypeEnum: number = 0, nested: boolean = true): Observable<any> {
let hosts;
let locations;
let hostAccessPoints;
let treeResult: Observable<any>;
if (nodeTypeEnum === NodeType.Location) {
// more code..
let someTree = getStuff();
return someTree;
}
if (nodeTypeEnum === NodeType.Host) {
// more code..
let someTree = getStuff();
return someTree;
}
if (nodeTypeEnum === NodeType.HostAccessPoint) {
let accPointTree;
if (sessionStorage["hostAccessPoints"]) {
// more code..
let someTree = getStuff();
return someTree;
}
else {
this.siteConfigService.getHostAccessPoints().subscribe(data => {
// more code..
let someTree = getStuff();
return someTree;
});
}
}
if (nodeTypeEnum === NodeType.HostStorage) {
// TO DO
}
}
**** 更新(基于下面提供的 Observable.of()
建议答案):
// Builds hierarchical treeview data
getTreeViewChildNodes(currNode: any, nodeType: string, nodeTypeEnum: number = 0, nested: boolean = true): Observable<any> {
let hosts;
let locations;
let hostAccessPoints;
if (nodeTypeEnum === NodeType.Location) {
if (sessionStorage["locations"] != null && sessionStorage["locations"] != "undefined") {
locations = JSON.parse( sessionStorage.getItem('locations') );
}
// find child nodes
let locationTree = this.findChildren(currNode, locations, nested);
return Observable.of(locationTree);
}
if (nodeTypeEnum === NodeType.Host) {
if (sessionStorage["hosts"] != null && sessionStorage["hosts"] != "undefined") {
hosts = JSON.parse( sessionStorage.getItem('hosts') );
}
// find child hosts for current node
let hostTree = this.findChildHosts(currNode, hosts, nested);
return Observable.of(hostTree);
}
if (nodeTypeEnum === NodeType.HostAccessPoint) {
let accPointTree;
if (sessionStorage["hostAccessPoints"]) {
hostAccessPoints = sessionStorage.getItem("hostAccessPoints");
accPointTree = this.findChildHostAccessPoints(currNode, hostAccessPoints, nested);
return Observable.of(accPointTree);
}
else { // **** THREW ERROR FROM CALLER 'Cannot read property 'subscribe' of undefined****`
/*this.siteConfigService.getHostAccessPoints().subscribe(data => {
hostAccessPoints = JSON.parse(data);
accPointTree = this.findChildHostAccessPoints(currNode, hostAccessPoints, nested);
return Observable.of(accPointTree);
});*/
// ** CORRECTED CODE **
return this.siteConfigService.getHostAccessPoints().map(data => {
hostAccessPoints = JSON.parse(data);
accPointTree = this.findChildHostAccessPoints(currNode, hostAccessPoints, nested);
return accPointTree;
});
}
}
}
从我的组件代码中,仅当我调用我的服务并进而调用http.get()时,它会抛出一个错误TypeError: Cannot read property 'subscribe' of undefined
:
this.configService.getTreeViewChildNodes(this.selectedTreeViewItem, type, nodetype, true).subscribe(data => {
this.rebuildNetworkTree(data);
});
getTreeViewChildNodes(currNode: any, nodeType: string, nodeTypeEnum: number = 0, nested: boolean = true)
。对于Subject
,订阅者将等待结果。这就是我困惑的地方,不知道如何像你建议的那样使用Subject
。 - bob.mazzo