路由器导航上不调用 LifeCycle 调用

LifeCycle calls are not called on router navigation

我设置了这条路线

{path:'home', component:HomeComponent, canActivate: [AuthGuard]},  
{path:'profile', component:UserProfileComponent, canActivate: [AuthGuard] },

在我的 navbar.component 中,我有以下内容

        <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact: true}">
            <a [routerLink]="['/home']" class="nav-link"><i class="fas fa-home fa-2x"></i></a>
        </li>
        <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact: true}">
            <a [routerLink]="[ '/profile']" class="nav-link"><i class="fas fa-user fa-2x"></i></a>
        </li>

在我的 home.component 中,我在 ngOnInit()

中从我的 firebase 服务获取数据
ngOnInit() {
    this.firebaseService.getPodcasts().subscribe(podcasts => {
      this.podcasts = podcasts;
    });

这只会在我第一次导航到主页时发生,但如果我转到配置文件(从导航栏)然后返回主页,则不会调用 ngOnInit(我尝试在控制台登录但没有任何反应)。但是当我写 URL 例如 https://localhost/4200/home 时,它确实激活了 ngOnInit() 生命周期。

我不确定这有多相关,但这是我的 HTML app.component :

<div class="app-container">

    <app-navbar *ngIf="authService.getAuthState()" (updateSideNav)='toggleSideNavParent($event)'>
    </app-navbar>
    <flash-messages></flash-messages>

    <router-outlet>
    </router-outlet>
</div>
<div *ngIf="authService.getAuthState()" class="sidenav-overflow" [ngClass]="{'show-overflow':isSideNavOpen}">
    <app-sidenav [isSideNavOpen]='isSideNavOpen'></app-sidenav>
</div>

在我的 home.component 中:

<div class="jumbotron">
<h1>home</h1>
    <app-record></app-record>
    <app-content [allPodcasts]="podcasts"></app-content>
</div

好的,所以问题不在于 ngOnInit,而在于 firebaseFirestore。 每当我加载 ngOnInit 并调用

this.firebaseService.getPodcasts().subscribe(podcasts => {
      this.podcasts = podcasts;
    });

它出于某种原因从未调用过它。但我将 firebaseService.getPodcasts() 从:

更改为
public getPodcasts(){
      return this.podcasts;
    }

收件人:

 public getPodcasts(){
  this.podcasts = this.podcastCollection.snapshotChanges().map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Podcast;
      data.id = a.payload.doc.id;
      return data;
    })
  })
  return this.podcasts;
}

现在它可以工作了..我仍然不明白为什么,因为我在 firebaseService 构造函数中设置了

this.podcasts = this.podcastCollection.snapshotChanges().map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Podcast;
      data.id = a.payload.doc.id;
      return data;
    })
  })