Location.subscribe()正在泄露

时间:2017-06-09 06:56:00

标签: angular

我使用Location.subscribe()来检测"返回"按钮。 https://angular.io/docs/ts/latest/api/common/index/Location-class.html

where()

然而,当我重复推动"返回"&" forward"按钮。 我认为Locaion.subscribe()正在泄漏。 但是我不知道如何发布它,因为Location类没有取消订阅()。

如何释放Location.subscribe()或防止泄漏?

我想过使用PlatformLocation.onPopState()。 https://angular.io/docs/ts/latest/api/common/index/PlatformLocation-class.html

但是,文档说明如下

export class PathLocationComponent {
  location: Location;
  constructor(location: Location) { 
     location.subscribe(val => console.log(val))//output popstate
  }
}

1 个答案:

答案 0 :(得分:1)

  

你可以试试这个:

import {Subscription} from 'rxjs/Subscription';

export class PathLocationComponent implements OnDestroy {
location: Location;
private subscription: Subscription;

      constructor(location: Location) {
        this.subscription = location.subscribe(val => console.log(val))//output popstate
      }

     ngOnDestroy(): void {
        this.subscription.unsubscribe();
     }
}