用这个代码
ngOnInit() {
this.router.events.subscribe((val) => {
if (this.router.url.indexOf('page') > -1) {
let id = this.activedRoute.snapshot.params['Id']
this.busy = this.httpCall.get('/pub/page/GetPageById/' + id)
.subscribe(data => {
this.pages = <Page[]>data
})
console.log(id)
}
})
}当我导航到domain.com/#/en/page/13时,它会按预期记录13。
但是,当我导航到id为27的页面时,它会记录:13 13 27。
当我导航回13日志时,拥有:27 27 13。
为什么会这样呢?
发布于 2017-05-29 13:30:02
当您破坏组件时,您必须取消订阅。
首先你必须
import { OnDestroy } from '@angular/core;'那你就得
export class myClass implements OnInit, OnDestroy {
myObserver = null;
// Rest of your code
}在你的ngOnInit里,你必须
ngOnInit() {
this.myObserver = this.router.events.subscribe(...);
}然后,创建一个函数
ngOnDestroy() {
this.myObserver.unsubscribe();
}https://stackoverflow.com/questions/44243653
复制相似问题