简体   繁体   English

如何在ionic 2中刷新页面或刷新ngOnInit()

[英]How to refresh a page or refresh ngOnInit() in ionic 2

i'm wondering if there is a way to refresh the ngOnInit() function or refresh the component.我想知道是否有办法刷新 ngOnInit() 函数或刷新组件。 i don't wanna use the pull to refresh method.我不想使用 pull to refresh 方法。 i want to do it on button click or tap.我想通过单击或点击按钮来完成。 Thank you谢谢

home.ts file home.ts 文件

  checkNetwork() {
  console.log("check internet");
this.platform.ready().then(() => {
  if(this.network.type == "none"){
    this.shouldHide = false;
    this.dividerHide = true;
  }
    let alert = this.alertCtrl.create({
        title: "Connection Status",
        subTitle: <string> this.network.type,
        buttons: ["OK"]
    });
  alert.present();
});
}

home.html file home.html 文件

<ion-card [hidden]="shouldHide">
<ion-card-header>
  <img src="img/sad.png" />
</ion-card-header>
<ion-card-content>
  <ion-card-title style="text-align:center">
    No INTERNET!
  </ion-card-title>
  <button ion-button full (click)="refreshPage($event)">Retry</button>
</ion-card-content>

when the connection will be available i want the page to refresh当连接可用时,我希望页面刷新

Thank you all, this is how i managed it 谢谢大家,这就是我管理它的方式

home.ts file home.ts文件

ngOnInit(){
 this.LoadData();}


checkNetwork() {
  console.log("check internet");
this.platform.ready().then(() => {
  if(this.network.type == "none"){
    this.shouldHide = false;
    this.dividerHide = true;
  }else{
    this.shouldHide = true;
    this.dividerHide = false;
    this.presentLoading()
  }
});
}

refreshPage()
  {this.LoadData();
}

public LoadData(){...}

Home.html file Home.html文件

<ion-card [hidden]="shouldHide">
<ion-card-header>
  <img src="img/sad.png" />
</ion-card-header>
<ion-card-content>
  <ion-card-title style="text-align:center">
    Pas de connexion internet!
  </ion-card-title>
  <button ion-button full (click)="refreshPage()">Reessayer</button>
</ion-card-content>

Use this code 使用此代码

refreshPage() {
   this.navCtrl.setRoot(this.navCtrl.getActive().component);
}

你可以通过调用ngOnInit()来重新初始化页面:this.ngOnInit();

My workaround for this was to present a toast (ToastController).我对此的解决方法是提供一个吐司(ToastController)。 Toast shows, toast disappears, then angular refreshes the data like it's supposed to. Toast 显示,toast 消失,然后 angular 像它应该的那样刷新数据。 Toast can say "Refreshing data" or whatever is relevant for you -- in my case "New message from blah@blah". Toast 可以说“刷新数据”或任何与您相关的内容——在我的例子中是“来自 blah@blah 的新消息”。 Or perhaps only show the toast for 1 ms if you don't want to clutter the UI.或者,如果您不想弄乱 UI,则可能只显示 1 毫秒的吐司。

Doing this.navCtrl.setRoot(this.navCtrl.getActive().component);this.navCtrl.setRoot(this.navCtrl.getActive().component); "works" but has the side effect of totally reseting the nav stack, which is usually totally unacceptable. “有效”但具有完全重置导航堆栈的副作用,这通常是完全不可接受的。

You may need to refactor your code structure that so it doesn't need the page refresh, using an init method or Rx's subscriptions. 您可能需要使用init方法或Rx的订阅来重构您的代码结构,以便它不需要页面刷新。

In case if you need the page reload, why don't you use reload method? 如果您需要重新加载页面,为什么不使用重载方法?

 window.location.reload(); 

Edited: 编辑:

Are you using network.onConnect subscription? 您使用的是network.onConnect订阅吗?

 // watch network for a connection let connectSubscription = this.network.onConnect().subscribe(() => { console.log('network connected!'); // We just got a connection but we need to wait briefly // before we determine the connection type. Might need to wait // prior to doing any api requests as well. setTimeout(() => { if (this.network.type === 'wifi') { console.log('we got a wifi connection, woohoo!'); } }, 3000); }); 

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

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