鉴于此代码:角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返回到“博客”组件 - 你怎么做? –