当我知道 observable 肯定会完成(通过 complete
或 error
通知)before 我的组件/类退出作用域,我还需要取消订阅它以防止内存泄漏吗?换句话说,完成/错误的 observable 是否自动清理,所以我不必担心?
最佳答案
Observable Contract 的订阅和取消订阅 部分关于你的问题是确定的。它指出:
When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription. Observers do not need to issue an Unsubscribe notification to end subscriptions that are ended by the Observable in this way.
Observable Termination 部分也提到了这一点:
When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription.
所以,不,没有必要取消订阅完成或错误的可观察对象。但是,这样做没有坏处。
关于angular - 我们是否需要取消订阅已完成/出错的可观察对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41334931/