angular - 嵌套的 Angular 订阅未触发
问题描述
我有 3 个订阅需要订阅。但是当我运行它时,只有 1 个或 2 个触发了 3 个。我什至尝试交换订阅的顺序,最后一个仍然没有被触发。我还尝试在constructor
我的角度组件和ngOnInit()
. 我得到同样的回应。
尝试1:
this.n.authorizations.subscribe(x => {
alert("1"); // ✅
this.n.pages.subscribe(y => {
alert("2"); // ❌
this.n.user.subscribe(user => {
alert("3"); // ❌
});
});
});
尝试2:
this.n.pages.subscribe(y => {
alert("1"); // ✅
this.n.authorizations.subscribe(x => {
alert("2"); // ✅
this.n.user.subscribe(user => {
alert("3"); // ❌
});
});
});
尝试 3:
this.n.user.subscribe(user => {
alert("1"); // ✅
this.n.authorizations.subscribe(x => {
alert("2"); // ✅
this.n.pages.subscribe(y => {
alert("3"); // ❌
});
});
});
我确实有这些服务的代码,但它们又长又复杂。可能与问题无关。
解决方案
从您遵循的方法来看,看起来任何订阅都不依赖于其他订阅。
所以在那种情况下,为什么不forkJoin
把它们再subscribe
加入 Observable。
import { forkJoin } from 'rxjs';
...
const combined = forkJoin(
this.n.user,
this.n.authorizations,
this.n.pages
);
...
combined.subscribe(
[user, auths, pages] => console.log(user, auths, pages),
error => console.log(error),
() => console.log('Combined Completed')
);
推荐阅读
- javascript - XMLHttpRequest 函数仅在最后一次调用时返回数据
- flutter - 如何在颤动中动态更改文本?
- performance-testing - 加特林循环中基于条件的退出
- tesseract - 提高微调 Tesseract 4 tessdata 文件的速度
- swift - 将多个键快速映射到单个编码键
- javascript - 将附加实例限制在动态内容上
- c++ - C++ 重载 << 运算符
- reactjs - npm 错误!意外的令牌 < 在 JSON 中的位置 0 解析时靠近 '
我正在尝试在命令行上安装 react
npm install react --save
并收到错误消息npm ERR! Unexpected token < in JSON at position 0 while parsing near '<!doctype html><htm...'
- imagemagick - 将 ColorToAlpha 精确复制到 MagickCore
- typescript - Firebase task.getResult().getData(); 总是返回 null。(异步问题?)