2017-04-03 39 views
0

我想知道是否有办法刷新ngOnInit()函数或刷新组件。我不想用拉来刷新方法。我想按钮点击或点击。谢谢如何刷新页面或刷新ngOnInit()离子2

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的文件

<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> 

当连接将可我想页面刷新

+0

这么叫上按一下按钮加载数据功能..不知道什么是这里的问题 –

+0

我;使用离子加载插件的功能从服务器下载数据,但有时数据下载失败,但微调持续。我想要一个可以重新启动页面的函数,以便函数和加载模块 –

+0

可以共享相关代码并编辑问题? –

回答

2

谢谢大家,这是我如何管理它

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文件中

<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> 

-2

您可能需要重构你的代码结构,因此它不需要页面刷新,使用init方法o R Rx的订阅。

如果您需要重新加载页面,为什么不使用重新加载方法?

window.location.reload();

编辑:

是否使用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); 
 
});

+0

做到了在离子2上工作?它会重新加载整个应用程序或只有组件? –

+0

是的,它会工作,因为ionic2应用程序正在铬浏览器下运行 – karser

+0

这是一个非常糟糕的做法,因为整个应用程序将被重新加载。任何会话数据也将丢失。 – JoeriShoeby

0

使用此代码

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