我使用Location.subscribe()来检测"back“按钮。https://angular.io/docs/ts/latest/api/common/index/Location-class.html
export class PathLocationComponent {
location: Location;
constructor(location: Location) {
location.subscribe(val => console.log(val))//output popstate
}
}但是,当我重复按“后退”和“向前”按钮时,日志的数量会增加。我认为Locaion.subscribe()在漏水。但是我不知道如何发布它,因为Location类没有取消订阅()。
如何释放Location.subscribe()或防止泄漏?
我想使用PlatformLocation.onPopState()。https://angular.io/docs/ts/latest/api/common/index/PlatformLocation-class.html
但是,文档说明如下
This class should not be used directly by an application developer. Instead, use Location.发布于 2017-06-09 07:06:37
你可以试试这个:
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();
}
}https://stackoverflow.com/questions/44451244
复制相似问题