如果我有一个包含 Observable 的服务并且我有以下代码...
this.collapse.newChild.subscribe(name => {
if (name === "helpArea") {
this.collapse.process("helpArea", true);
}
// I would like to detach now
});
是否可以在不取消原始订阅的情况下分离?如果我调用 unsubscribe
,那么当我调用 this.collapse.newChild.next("thing")
时,它会抛出错误 Error: object unsubscribed
。
所以我不认为取消订阅是我要找的,那么有没有办法分离单个观察者?
目前我正在使用...
this.collapse.newChild.map(name => name === "helpArea").first().subscribe(...)
最佳答案
Peter 的回答在设计/实践 PoV 中是正确的,但也请注意,您会收到此错误,因为您取消订阅
主题。
您想要停止的是订阅:
var subscription = this.collapse.newChild.subscribe(...)
// NOT this
// this.collapse.newChild.unsubscribe()
// this
subscription.unsuscribe()
这不会终止/停止主题,而只会终止/停止您当前的订阅。
关于javascript - 是否可以从 Observable 中只删除一个 RXJS Observer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41727245/