Is it necessary to unsubscribe from observables created by Http methods?

前端 未结 9 1673
醉酒成梦
醉酒成梦 2020-11-22 04:50

Do you need to unsubscribe from Angular 2 http calls to prevent memory leak?

 fetchFilm(index) {
        var sub = this._http.get(`http://example.com`)
              


        
9条回答
  •  心在旅途
    2020-11-22 05:01

    RxJS observable are basically associated and work accordingly you subscribe it. When we create the observable and the movement we complete it, observable automatically gets closed and unsubscribed.

    They work in the same fashion of the watchers but quite different order. The better practice to unsubscribe them when component is getting destroy.We could subsequently do that by eg. this.$manageSubscription.unsubscibe()

    If we have created the observable like below mentioned syntax such as

    ** return new Observable((observer) => { ** // It makes observable in cold state ** observer.complete() **}) **

提交回复
热议问题