Angular/RxJs When should I unsubscribe from `Subscription`

前端 未结 22 2247
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 04:56

When should I store the Subscription instances and invoke unsubscribe() during the NgOnDestroy life cycle and when can I simply ignore them?

22条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 05:15

    Another short addition to the above mentioned situations is:

    • Always unsubscribe, when new values in the subscribed stream is no more required or don't matter, it will result in way less number of triggers and increase in performance in a few cases. Cases such as components where the subscribed data/event no more exists or a new subscription to an all new stream is required (refresh, etc.) is a good example for unsubscription.

提交回复
热议问题