2017-10-07 43 views
2

我有一个Web应用程序。在我的右侧栏中,我有3个帖子。 当我点击后,我有一个导航:角ngOnInit - 刷新所需

this.router.navigate(['../post', this.post.id]); 

有一个后档组件将接管。 此组件具有一个函数,该函数可以从使用指定标识获取我的帖子的服务调用函数。现在,我将在我的内容区域(屏幕的左侧部分)中看到该帖子的所有详细信息。该函数(获取我的文章)仅在Post Profile组件的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); 
} 
+0

你真的必须展示你的实际代码,没有人能猜到你的代码是怎样的。 [如何创建一个最小,完整和可验证的示例](https://stackoverflow.com/help/mcve) – Alex

回答

1

在您的文章轮廓组件的ngOnInit方法你已经订阅ActivatedRouteparams观察到。

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) 
      } 
     }); 
    } 
+0

你怎么知道OP的代码看起来不是这样?问题可能在任何地方,这就是为什么我要求代码。也许你是对的,谁知道。 – Alex

+0

我认为他想要通用解决方案,而这正是我试图在此给出的。但是,如果@Marian给我们一些他实现的代码,那么我会相应地更新我的答案。 – asmmahmud

+0

@Marian,不客气。如果我的解决方案有效,那么如果您接受我的解决方案作为正确答案,对他人会有所帮助。 – asmmahmud