【问题标题】:BehaviourSubject not updating html template Ionic 5BehaviorSubject 未更新 html 模板 Ionic 5
【发布时间】:2021-11-07 19:11:22
【问题描述】:

我有一个用户到另一个用户的消息列表,并希望在 Ionic 5、Angular 11 中实时更新它们。

我正在使用 BehaviourSubject,最后一次更改显示在构造函数上。 在 ngOninit 它不可能查看更改(订阅)

mesage.ts

  private categorySelectedSubject = new BehaviorSubject<any>();
  categorySelectedAction$ = this.categorySelectedSubject.asObservable();


   constructor(){
  this.categorySelectedAction$.subscribe(x => console.log("data here ", x))

  this.categorySelectedAction$ = this.chatService.getSpecificMessage(request)
}

     
  

  async sendMsg(savemessage) {
   this.categorySelectedSubject.next(savemessage)
  }
  <ion-list id="list" *ngIf="categorySelectedAction$  | async as products">
  
      <div  *ngFor="let chat of products['response']; let i = index; " >
      
      ..... ect ....

构造函数的subscritpion 的结果只是最后一个:savemessage 并且没有以前的数据。 html上的模板完全没有变化。

也尝试过 NgZone 和 ChangeDetectorRef 但没有任何效果

【问题讨论】:

  • 这里的request 是什么?

标签: angular ionic-framework rxjs ionic3 ionic4


【解决方案1】:

您必须使用 Observable 来获取数据,然后与 BehaviourSubject 结合以进行 CRUD 操作。

看看 merge 和 combineLast 运算符。

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多