我有一个需求,需要在当前页面的刷新按钮被点击时刷新当前页面的内容。我查看了一些网站,但对这个功能不是很清楚。有谁能建议我如何实现这个功能?
<button (click)="refreshPage()">Refresh Page</button>
Typescript
ionViewWillEnter(){
this.myDefaultMethodToFetchData();
}
refreshPage() {
this.ionViewWillEnter();
}
myDefaultMethodToFetchData(){
...Do Something, when the page opens...
}
在构造函数中添加此代码,然后将页面名称替换为您的页面名称
this.navigationSubscription = this.router.events.subscribe((e: any) => {
if (e instanceof NavigationEnd && e.url === '/page-name' && localStorage.getItem('prevUrl') === '/page-name') {
this.ionViewWillEnter();
this.ngOnInit
}
});
这个会起作用!!! 将其放在按钮点击事件调用的函数下面。
this.navCtrl.setRoot(this.navCtrl.getActive().component);
或者
在您的页面上使用ionViewWillEnter生命周期事件。 您可以将正确的代码放入该方法中,以刷新页面上所有必要的内容。 https://ionicframework.com/docs/api/navigation/NavController/
this.navCtrl.pop(); this.navCtrl.push(NewPage);