鑑於此代碼:角2路由訂製改變檢測
ngOnInit() {
this.apiService.getBlogPosts().subscribe(blogPosts => {
this.allBlogPosts = blogPosts;
this.changeDetector.detectChanges();
}));
}
我試圖理解爲什麼我不得不使用this.changeDetector.detectChanges();
的情況是,如果我打開我的博客路線的第一次,我的帖子加載罰款,如果我route
到另一個頁面(或者其他component
或相同的「博客」 component
顯示某個帖子的細節(如「博客/我-後」)),那麼我route
回「博客「組件('blog /'),這次模板無法更新與職位。我可以在我的Inspect
,Network
標籤看到,該路由得到的職位仍在工作,console.log
是表示一切似乎順利通了,但模板沒有更新,除非我強迫變化檢測。這是預期的行爲?可以補救嗎?這似乎很奇怪...
另一個值得注意的事情是,無論是第一次和3時,我參觀「博客」 component
我用:
this.router.navigate([`/${url}`]);
來找到它......而這沒法子......
編輯:的getBlogPosts()
方法是我ApiService
,我注入我的BlogComponent
:
public getBlogPosts() : Observable<BlogPost[]> {
return this.http.get(`${ environment.apiUrl }GetBlogPosts`, { headers: this.headers })
.map((response: Response) => <BlogPost[]>response.json());
}
是什麼'this.apiService.getBlogPosts() '?顯示代碼 –
當然,看看編輯。你的時間非常感謝。 –
_I返回到「博客」組件 - 你怎麼做? –