Get all current (active) subscriptions

后端 未结 7 1113
清歌不尽
清歌不尽 2021-02-12 20:58

Is it possible to get all the \"active\" subscriptions without storing them manually?

I\'d like to unsubscribe all of the \"active\" subscriptions and don\'

7条回答
  •  遇见更好的自我
    2021-02-12 21:33

    You can just store one Subscription, and add to it.

    private _subscriptions = new Subscription();   // yes you can do this!
    

    Then add to it

    _subscriptions.add(
        this.layoutManager.width$.subscribe((w) => {
    
            // any nested subscriptions can also use this mechanism
            _subscriptions.add(...);
        });
    

    Then in ngOnDestroy() you just call _subscriptions.unsubscribe();.

    I've been trying to make a 'clever' way to do this, but to be frank I've overengineered it quite a bit (including logging) and this is the simplest way I've come across.

提交回复
热议问题