【问题标题】:The Firebase database subscription is not closed after the change reference更改参考后未关闭 Firebase 数据库订阅
【发布时间】:2018-02-21 23:47:24
【问题描述】:

在我的 firebase 应用程序中,会有加载更多按钮,当用户单击它时,预计会加载更多内容。我为此使用了一个侦听器,但在加载更多内容后我无法关闭。例如,当用户点击“加载更多”按钮时,将会有更多订阅,并且旧订阅也会被触发。

export class PostComponent {
  private sub: Subscription;
  private counter = 50;
  constructor(private afm: AngularFireDatabase) {}
  loadPosts() {
    this.sub = this.afm.list("posts", { query: { limitToFirst: this.counter } })
      .subscribe((posts: any[]) => this.posts = posts);
  }

  loadMore() {
    this.sub.unsubscribe();
    this.counter += 50;
    this.loadPosts();
 }
}

【问题讨论】:

    标签: javascript angular firebase-realtime-database angularfire2


    【解决方案1】:

    您是否尝试在加载后立即取消订阅,即在订阅回调中。

    试试这个

    loadPosts() {
      this.sub = this.afm.list("posts", { query: { limitToFirst: this.counter}})
         .subscribe((posts: any[]) => {
             this.posts = posts;
             this.sub.unsubscribe();
          });
    }
    

    【讨论】:

    • 我们这样做之后,就像曾经的听众一样。发生变化时不会触发。正是我想在用户单击“加载更多”按钮后关闭订阅。
    猜你喜欢
    • 2021-06-09
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2020-06-20
    相关资源
    最近更新 更多