Điều này xảy ra khi bạn viết "this.router.events" trên ngOnInit hoặc Constructor, Để khắc phục sự cố này, bạn nên viết nó trên một hàm khác như sau:
onRouteChange () {
this.router.events.subscribe((event) => {
if (event instanceof NavigationStart) {
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);
}
}
})
}
nhưng có một vấn đề khác trong đó bạn nên xem xét khi sử dụng "this.router.events.subscribe", mọi thứ bạn đã viết ra trong hàm này sẽ xảy ra khi bạn điều hướng trong các trang vì vậy tốt hơn nên sử dụng dòng này để ngăn chạy nó trên mọi thay đổi đường dẫn:
import {Router, NavigationStart} from '@angular/router';
onRouteChange () {
this.router.events.subscribe((event) => {
if (event instanceof NavigationStart) {
// this line
if(event.url == 'your_page_path')
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);
}
}
})
}