【发布时间】:2019-12-04 12:12:56
【问题描述】:
在角度我想创建一个多订阅序列。当第一次订阅完成后,再进行下一次订阅。与此类似。
this.globalCtrl.getSon().subscribe(
response => {
this.son= response;
},
error => {
console.log(error);
},
() => {
//DOESN´T ENTER IN THIS SUBSCRIBE
this.dashboardCtrl.getMarkers(this.son).subscribe(
response => {
this.markers = response["body"]["data"];
if (this.markers.length == 0) {
this.toast.fire({
type: "error",
title: "No hay datos geográficos del grupo seleccionado"
});
}
this.timezone = response["body"]["timezone"];
},
error => {
console.log(error);
}
);
问题:没有进入第二次订阅并且“响应”有数据。 ¿ 有谁知道我该怎么做?
更新
如果我将第二个订阅放在这样的响应中
this.globalCtrl.getSon().subscribe(
response => {
this.son= response;
this.dashboardCtrl.getMarkers(this.son).subscribe(
response => {
this.markers = response["body"]["data"];
if (this.markers.length == 0) {
this.toast.fire({
type: "error",
title: "No hay datos geográficos del grupo seleccionado"
});
}
this.timezone = response["body"]["timezone"];
},
error => {
console.log(error);
}
);
有效,但视图不显示数据。当我刷新视图数据加载正确,但在第一次加载不加载数据。
更新 2:使用开关图
this.globalCtrl.getfather().pipe(
switchMap(son=> this.dashboardCtrl.getMarkers(son.toString()).subscribe( /ERROR
response => {
this.markers = response["body"]["data"];
if (this.markers.length == 0) {
this.toast.fire({
type: "error",
title: "No hay datos geográficos del grupo seleccionado"
});
}
this.timezone = response["body"]["timezone"];
},
error => {
console.log(error);
}
)
);
属性订阅在 OperatorFunction 中不存在
可能是可观察到的导致错误
getFather(): Observable<any> {
return this.grupo.asObservable();
}
【问题讨论】:
-
你不应该在同一个
subscribe()块中嵌套多个subscribe()方法!在处理可观察对象时,这是非常糟糕的做法。相反,您应该使用 RxJS 运算符,例如mergeMap、switchMap,甚至forkJoin,具体取决于要求和数据流。你也可以参考这个。 Angular Subscribe within Subscribe