简体   繁体   English

嵌套的角度订阅不会被触发

[英]Nested Angular Subscriptions not Firing

I have 3 subscriptions I need to subscribe to. 我有3个订阅我需要订阅。 But when I run it, only 1 or 2 of them fire out of 3. I even tried swapping the order of the subscriptions and the last one still doesn't get fired. 但是当我运行它时,只有1或2个触发3.我甚至尝试交换订阅的顺序,最后一个仍然没有被解雇。 I also tried running these in the constructor of my angular component and in ngOnInit() . 我也尝试在我的角度组件的constructorngOnInit()运行它们。 I get the same response. 我得到了同样的回应。

Attempt 1: 尝试1:

this.n.authorizations.subscribe(x => {
    alert("1"); // ✅
    this.n.pages.subscribe(y => {
        alert("2"); // ❌
        this.n.user.subscribe(user => {
            alert("3"); // ❌
        });
    });
});

Attempt 2: 尝试2:

this.n.pages.subscribe(y => {
    alert("1"); // ✅
    this.n.authorizations.subscribe(x => {
        alert("2"); // ✅
        this.n.user.subscribe(user => {
            alert("3"); // ❌
        });
    });
});

Attempt 3: 尝试3:

this.n.user.subscribe(user => {
    alert("1"); // ✅
    this.n.authorizations.subscribe(x => {
        alert("2"); // ✅
        this.n.pages.subscribe(y => {
            alert("3"); // ❌
        });
    });
});

I do have the code for these services, but they're long and complicated. 我确实有这些服务的代码,但它们很长很复杂。 Probably not related to the problem. 可能与问题没有关系。

Judging from the approaches you're following, looks like any subscription doesn't depend on the other subscriptions. 从您所遵循的方法来看,看起来任何订阅都不依赖于其他订阅。

So in that case, why not forkJoin them and then subscribe to the joined Observable. 所以在那种情况下,为什么不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')
);

I figured out the problem (kind of). 我想出了问题(种类)。 On the first attempt, the subscription to pages occured after the pages observable resolved. 在第一次尝试时,订阅pages后发生pages观察的解决。 So I changed the return object in that service function to a ReplaySubject instead of a Subject , which causes late subscribers to get the most recent value emitted. 所以我将该服务函数中的返回对象更改为ReplaySubject而不是Subject ,这会导致后期订阅者获取最新发布的值。 I don't know how all this happened but this resolved my problem. 我不知道这一切是怎么发生的,但这解决了我的问题。 It doesn't make sense though. 但它没有意义。

Edit: As another user suggested, BehaviorSubject would be better in this case. 编辑:正如另一位用户建议的那样,在这种情况下,BehaviorSubject会更好。

It looks to me like you are chaining things that shouldn't be chained. 它看起来像你在链接不应该链接的东西。

this.n.user.subscribe(user => {
    alert("1");
});
this.n.authorizations.subscribe(x => {
    alert("2");
});
this.n.pages.subscribe(y => {
    alert("3");
});

When you subscribe like you have, your inner subscriptions aren't even attached until your outer subscriptions receive their first value. 当您订阅时,您的内部订阅甚至不会附加,直到您的外部订阅收到他们的第一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM