多个switchmap,我应该退订吗?

发布于 2025-02-13 12:37:46 字数 816 浏览 1 评论 0原文

这里一个例子:

这种方法正确吗? 我应该在每个 switchmap 退订吗?我应该在每个 switchmap 之后打电话 takeuntil 吗?

this.sharedSrv.postDetail.pipe(
  switchMap(post => {
    if (post) {
      this.hasPost = true;
      this.post = post;
    }

    this.viewedMainComment = null;
    this.viewedSubComments = [];
    return this.userSrv.getUserAsObservable();
  }),
  takeUntil(this.destroyFirstSwitchMap$), 
  switchMap(user => {
    if (user) {
      if (this.post.user.id == user.id) this.isOwnPost = true;
      this.user = user;
      console.log(user);
      return this.postsSrv.getPostInteraction(this.user.id, this.post.id, this.post.user.id);
    }
    return of(null); 
  }),
  takeUntil(this.destroySecondSwitchMap$),
).subscribe( );

Here an example:

Is this approach correct?
Should I unsubscribe in each switchMap? Should I call takeUntil after each switchMap?

this.sharedSrv.postDetail.pipe(
  switchMap(post => {
    if (post) {
      this.hasPost = true;
      this.post = post;
    }

    this.viewedMainComment = null;
    this.viewedSubComments = [];
    return this.userSrv.getUserAsObservable();
  }),
  takeUntil(this.destroyFirstSwitchMap$), 
  switchMap(user => {
    if (user) {
      if (this.post.user.id == user.id) this.isOwnPost = true;
      this.user = user;
      console.log(user);
      return this.postsSrv.getPostInteraction(this.user.id, this.post.id, this.post.user.id);
    }
    return of(null); 
  }),
  takeUntil(this.destroySecondSwitchMap$),
).subscribe( );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2025-02-20 12:37:46

是的,一个Takeuntil将结束级联中的所有订阅。

Yes, one takeUntil will end all subscriptions in cascade.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文