我有一个网络应用程序。在我的右边酒吧,我有3个职位。当我点击一个帖子时,我有一个导航:
this.router.navigate(['../post', this.post.id]);在那里,职位配置文件的组成部分将接管。这个组件有一个函数,该函数从服务中调用一个函数,该服务使用指定的id获取我的帖子。现在,我将在我的内容区域(屏幕的左边部分)看到该帖子的所有细节。此函数(获取我的帖子)仅在Post配置文件组件的ngOnInit中调用。
如果我单击右侧栏中的另一篇文章,我可以看到url中的变化,但是获取我的帖子的函数没有被调用,我的内容区域的帖子细节也没有改变。如果我现在刷新页面,我可以看到我想要的帖子,一切都很好。
我的函数上有.subscribe,如果这有帮助的话。
我看不出有什么问题。
这是我的PostProfileComponent
constructor(postsService: PostsService, route: ActivatedRoute ) {
this.postsService = postsService;
this.routeSubscription = route.params
.subscribe((params: any) => {
this.postId = params['id'];
});
}
public getPostById(id: any): void {
this.postsService.getPostById(id)
.map((response: Post) => {
this.post = response;
console.log(this.post);
})
.catch((error: any) => {
return error;
})
.subscribe();
}
ngOnInit(): void {
this.getPostById(this.postId);
}发布于 2017-10-07 16:48:33
在Profile组件的ngOnInit方法中,您必须订阅可观察到的ActivatedRoute params。
subs1: Subscription;
constructor(private route: ActivatedRoute){}
ngOnInit() {
this.subs1 = this.route.params
.subscribe((params: Params) => {
const postId = params['id'] ? +params['id'] : '';
if(postId){
// call the function (that gets the post)
}
});
}https://stackoverflow.com/questions/46622077
复制相似问题