Unsubscribing an array of subscriptions

后端 未结 2 678
粉色の甜心
粉色の甜心 2021-02-02 15:40

In an Angular app, I\'m managing my subscriptions by pushing them all into an array, then looping through it and unsubscribing during ngOnDestroy.

p         


        
2条回答
  •  醉梦人生
    2021-02-02 16:15

    subs: Subscription[] = [];
    
    ngOnInit(): void {
        this.subs.push(someObservable.subscribe(foo => bar));
    }
    
    ngOnDestroy(): void {
        this.subs.map(s => s.unsubscribe);
    }
    

提交回复
热议问题