我的應用程序設置爲在頂部點擊鏈接時過濾不同的項目。這通過接收一個路由參數並將其傳遞給我的組件完成。我的組件然後根據提供的參數篩選數據庫查詢OnInit。這一切都很好,但我遇到的問題是,當我在子路由之間移動時,ngOnInit並未被解僱。ngOnInit不發射兒童路線之間的一個變化
ngDoCheck似乎是我需要使用的東西,但是當應用程序正在加載並且因爲projectService未完成而破壞我的代碼時,它會被解僱。有沒有辦法強制ngDoCheck等到項目加載之後纔開始監聽? Angular網站表示它應該已經這樣做了,但是從我所看到的不是。
任何意見我應該如何正確實施這個?
組件
ngOnInit() {
this.paramsSub = this.activatedRoute.params.subscribe(params => this.pageCategory = params['id']);
this.getProjects();
}
getProjects() {
if (this.pageCategory) {
this.projectService.getProjects()
.subscribe(
projects => this.projects = projects.filter(project => project.category.includes(this.pageCategory)),
error => this.errorMessage = <any>error);
} else {
this.projectService.getProjects()
.subscribe(
projects => this.projects = projects,
error => this.errorMessage = <any>error);
}
}
路線
const routes: Routes = [
{ path: '',
children: [{
path: ':id',
component: HomeComponent,
}]
},
{ path: '', component: HomeComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
謝謝!我能夠把你的話說出來並弄清楚。我仍然從OnInit調用所有的東西。 OhChanges似乎殺了我的查詢請求並且什麼也沒有返回。 – Jleibham
你是怎麼做到的? – chrismarx