如何在点击时刷新页面(ionic v4)

时间:2019-10-05 13:51:44

标签: angular ionic-framework

当用户删除数据时尝试刷新页面。页面上的某些Html元素会自动更新,但是主应用程序组件会保留旧数据,直到手动刷新页面为止。

ionViewWillEnter()不再如本示例所示工作:How to refresh page in ionic 4

我尝试改用ngOnInit(),但这没用。我也尝试过ChangeDetectorRef,但没有任何乐趣。

打字稿


import { ChangeDetectorRef } from '@angular/core'

public contentId: any;
public content: any;

constructor(private changeRef: ChangeDetectorRef) {
  ...
}

ngOnInit() {
  this.fetchData();
}

fetchData() {
  ...
  this.database.get(contentId).subscribe( content => {
    this.content = content[0];
 });
}

deleteData() {
  this.content.delete(id);
  this.changeRef.detectChanges();
}

HTML

<app-content *ngIf="contentId" [pageContent]="contentId"></app-content>

来自 this.content

的数据
{
 id: 'contentId',
 text: 'some text',
 author: 'username',
 uid: 'userId'
}

1 个答案:

答案 0 :(得分:0)

尝试使用 ionViewWillEnter

  ionViewWillEnter() {
        this.fetchData();
  }