在不重新加载应用程序的情况下清空应用程序中的所有可观察到

发布于 2025-02-13 07:04:28 字数 106 浏览 1 评论 0原文

当前,在我们的应用中,我们正在使用location.reload作为清除所有以前数据的所有可观察到的方法。

是否还有其他少一些黑客的方法,例如不重新加载整个应用程序或推荐的一些RXJ?

Currently, in our app we are using location.reload as a sure way to clear all observables of any previous data.

Is there any other less hacky way of doing this like without reloading the whole app or some rxjs recommended way?

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

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

发布评论

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

评论(2

歌入人心 2025-02-20 07:04:29

在Angular应用程序中处理可观察到的正确方法是在不再需要的情况下取消订阅。

一般而言,我们订阅了组件内可观察的一个。因此,当您破坏组件时,必须取消订阅订阅,如下所示:

class MyComponent implements OnInit, OnDestroy {
    // Variable used to hold subscription
    private subscription: Subscription;

    ngOnInit() {
       // Save subscription in a variable
       this.subscription = anyObservable.subscribe(...);          
    }

    ngOnDestroy() {    
      // Unsubscribe when the subscription is no more needed
      this.subscription.unsubscribe();
    }
    
    ...
}

可以观察到可以在整个应用程序中生存下来,而其他人必须尽快取消订阅,但通常在OnInit上订阅并在Ondestroy方法上取消订阅解决大多数问题的好方法。

The correct way to handle observables in an angular application is to unsubscribe when they are no more needed.

Generally speaking we subscribe to an Observable inside a component. So when you destroy the component you must unsubscribe the subscription as follow:

class MyComponent implements OnInit, OnDestroy {
    // Variable used to hold subscription
    private subscription: Subscription;

    ngOnInit() {
       // Save subscription in a variable
       this.subscription = anyObservable.subscribe(...);          
    }

    ngOnDestroy() {    
      // Unsubscribe when the subscription is no more needed
      this.subscription.unsubscribe();
    }
    
    ...
}

There can be are observables that survive through the whole application lifetime, while others must be unsubscribed as soon as possible, but often subscribing on the onInit and unsubscribing on the onDestroy methods is a good approach to solve most of the problems.

秋风の叶未落 2025-02-20 07:04:29

一旦完成责任,请尝试取消订阅订阅的可观察到的可观察。

try to unsubscribe the subscribed observable, Once there responsibility has been completed.

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