【发布时间】:2020-12-04 21:43:04
【问题描述】:
所以我在 rxjs 方面是新的,我知道我的问题的答案可能涉及到管道。但是,我已经这样做了,但从来没有工作过。我的方法是在地图中绘制路径like this
为此,我需要从我的数据库中获取所有行并获取linePaths like this 的数组。之后我在该数组中获取 pathKey,然后我 findByKey 路径并获取 pathObjects 列表。每个 pathObject 都包含一个数组 pathNodes like this.. 所以我要做的是进入每个 pathNode 并通过 pathNode 包含的键获取 NodeObject。我这样做是因为每个节点都包含要在地图上标记的纬度和经度。例如,在最后一张图片中,我发送 Node1 连接到 Node:15 连接到 Node:13 等等。问题是我试图在 forEach 中进行订阅,因此我的程序有点爆炸。当我 F5 时显示了许多解决方案,当我尝试通过 pathNodes 中的最后一个节点结束路径时,它让我将该列表的 lastNode 连接到另一个 PathNodeArray 的开始节点。
我要展示我的代码示例
this.lineService.getAll()
.subscribe(
data => {
this.lines = data;
//console.log(this.lines);
this.lines.forEach((element: any) => element.linePaths[0].linePath.forEach((element: any) => {
//console.log(element);
this.pathService.findByKey(element.path)
.subscribe(
data => {
this.paths = data;
//console.log(this.paths);
let observables: Observable < any > [] = [];
this.paths.pathNodes[0].pathNode.forEach((element: any) => {
//console.log(element);
observables.push(element.node);
})
const arrayToStore: any = [];
observables.forEach((element: any) => {
this.nodeService.findByKey(element)
.subscribe(
(data: any) => {
this.nodeToSearch = data;
arrayToStore.push(data);
for (let i = 0; i < arrayToStore.length - 1; i++) {
L.polyline([
[arrayToStore[i].latitude, arrayToStore[i].longitude],
[arrayToStore[i + 1].latitude, arrayToStore[i + 1].longitude]
]).addTo(this.map);
}
});
})
}
)
}));
});
我的问题显示的示例是这个:
observables.forEach((element: any) => {
this.nodeService.findByKey(element)
.subscribe(
(data: any) => {
this.nodeToSearch = data;
arrayToStore.push(data);
for (let i = 0; i < arrayToStore.length - 1; i++) {
L.polyline([
[arrayToStore[i].latitude, arrayToStore[i].longitude],
[arrayToStore[i + 1].latitude, arrayToStore[i + 1].longitude]
]).addTo(this.map);
}
}
);
})
【问题讨论】:
标签: javascript angular rxjs