为什么我的订阅在订阅了六个新值后停止工作?

Why does my subscription stops working after subscribing to exactly six new values?

我有一个posts.component。在posts.component.html中,我有以下

<ngx-masonry  class="masonry" [options]="myOptions" [updateLayout]="hasPostOpened">
  <div ngxMasonryItem class="masonryItem" *ngFor="let post of posts;index as idx" (click)="showPostDetail(post)" >
    <div class="crop">{{post.description}}</div>
    <img [src]="imgConstructor(post.img)">
  </div>
  <div ngxMasonryItem class="masonryItem masonryUser" *ngFor="let user of usersToDisplay" (click)="navigateToProfile(user.id)" >
    <img [src]="userImgConstructor(user.profileImage)">
    <div class="crop-user">{{user.username}}</div>
  </div>
</ngx-masonry>
</div>

<div class="detail" *ngIf="hasPostOpened">
    <router-outlet></router-outlet>
</div>

函数showPostDetail(post)定义如下:

showPostDetail(post) {
  this.sharedSrv.getHasPostOpened().subscribe(hasOpened => {
    if(hasPostOpened)
    this.router.navigate(['/post-detail/' + post.id]);
    else {
      this.sharedSrv.setHasPostOpened(true);
     this.router.navigate(['/post-detail/' + post.id]);

    }
  })
  
}

因为我没有重新渲染 post-detail.component,所以我将 post 的 id 传递给路由并订阅post-detail.component 中的路由,因此我可以获得 post 的 ID 并为新 [=51 构建新视图=]:

async ngOnInit(){
   this.route.paramMap.subscribe(async params => {
      this.waitUntilDataLoaded = false;
      this.subComments = [];
      this.post = {}
      let post_id = params.get('id');
      await this.publicSrv.getSinglePost(post_id).toPromise().then(post => {
        this.post = post;
        console.log(post);
      })
      await this.publicSrv.getCommentsFromPost(post_id).toPromise().then(async mainComments => {
        this.mainComments = mainComments;
        for(let mainComment of this.mainComments){
          this.subComments.push(await this.publicSrv.getSubComments(this.post.id, mainComment.commentator_id, mainComment.id).toPromise())
        }})

       this.waitUntilDataLoaded = true;
    })
}

我可以正确打开 6 个 post,如果我尝试打开第七个 post,订阅每次都会停止工作。为什么在详细视图中打开 6 post 秒后订阅总是停止工作?

编辑:

getSinglePost()

getSinglePost(p_id){
    return this.http.get(this.publicUrl + 'get/single-post', {params: {p_id: p_id}});
  }

getSubComments()

getSubComments(p_id, commentator_id, row_id){
    return this.http.get(this.publicUrl + "get/sub-comments", {params: {p_id: p_id, commentator_id: commentator_id, row_id: row_id}});
  }

getCommentsFromPost()

getCommentsFromPost(p_id){
    return this.http.get(this.publicUrl + "get/main-comments", {params: {p_id: p_id}})
  }

你能不能像这样包装你的订阅:

async ngOnInit(){
   this.route.paramMap.subscribe({
     next: async params => {
      this.waitUntilDataLoaded = false;
      this.subComments = [];
      this.post = {}
      let post_id = params.get('id');
      await this.publicSrv.getSingePost(post_id).toPromise().then(post => {
        this.post = post;
        console.log(post);
      })
      await this.publicSrv.getCommentsFromPost(post_id).toPromise().then(async mainComments => {
        this.mainComments = mainComments;
        for(let mainComment of this.mainComments){
          this.subComments.push(await this.publicSrv.getSubComments(this.post.id, mainComment.commentator_id, mainComment.id).toPromise())
        }})

       this.waitUntilDataLoaded = true;
    }),
     complete: () => console.log('completed')
   }
}

如果它在控制台中打印 'completed',则表示可观察对象已完成。
我可能已经做了一个类型,以确保它应该遵循以下模板:

  .subscribe({
    next: // your usual subscription function,
    complete: () => console.log('completed')
  })