简体   繁体   English

如何在 Ionic 3 (Ionic, Angular 5) 上刷新页面

[英]How to refresh a page on Ionic 3 (Ionic, Angular 5)

I'm building a favorite list which I can erase list items within a page.我正在构建一个收藏夹列表,我可以删除页面中的列表项。 It's based on Ionic sqlite storage.它基于Ionic sqlite存储。

I found Ionic page will keep removed ion list items until I re-enter the list page.我发现 Ionic 页面会保留已删除的离子列表项,直到我重新进入列表页面。 So it needs refreshing a page upon removing an item from ionic storage.因此,它需要在从离子存储中删除项目时刷新页面。

How can I refresh a particular page?如何刷新特定页面? window.location.refresh() seems to refresh the entire pages… this is not what I'm trying to do. window.location.refresh()似乎刷新了整个页面……这不是我想要做的。

I need to refresh a single page out of twenty pages on my app so navCtrl.setRoot() will not work either.我需要在我的应用程序上刷新 20 个页面中的一个页面,因此navCtrl.setRoot()也不起作用。

Thanks in advance,提前致谢,

请试试这个?

this.navCtrl.setRoot(this.navCtrl.getActive().component);

If you're going for a list of items, I would suggest displaying them with an ngFor directive.如果您要查看项目列表,我建议使用 ngFor 指令显示它们。 That way when you update the list, it will automatically be updated where it is displayed rather than having to refresh the page or anything like that.这样,当您更新列表时,它会自动更新到显示位置,而不必刷新页面或类似的内容。 Here's a short example:这是一个简短的例子:

HTML HTML

<ion-content>
 <ion-list> 
  <div *ngFor="let item of items">
    <p>{{item.name}}</p>
  </div>
  <button (click)="deleteFromArray()">Delete From Array</button>
  <button (click)="addToArray()">Add To Array</button>
 </ion-list> 
</ion-content>

TYPESCRIPT打字稿

items: any[] = [{name:'Penguin'},{name:'Seal'},{name:'Lion'}];

deleteFromArray() {
  this.items.pop();
}

addToArray() {
  this.items.push({name: 'Whale'});
}

Hope this helps!希望这可以帮助!

use NgZone.使用 NgZone。 It will refresh the component.它将刷新组件。

import { NgZone } from '@angular/core';

constructor(private zone: NgZone) {}

refresh() {
  this.zone.run(() => {
    console.log('force update the screen');
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM