在 ionic2 中刷新页面

Refresh a page in ionic2

有没有办法只刷新一个页面,即 ionic2 中只有一个屏幕。

我试过了:

window.location.reload();

location.reload();

但它会重建应用程序..有没有办法只刷新该页面(特定屏幕)。

也尝试过:

<ion-input *ngIf="no_internet === 1" (click)="refresh($event)"></ion-input>

在 TypeScript 中:

refresh(refresher) {
    console.log('Begin async operation', refresher);

    setTimeout(() => {
        console.log('Async operation has ended');
        refresher.complete();
    }, 2000);
}

试试这个:$window.location.reload(); $route.reload() use to reload route.

如果您正在使用 $stateProvider : $state.go($state.current, {}, {reload: true});

var currentPageTemplate = $route.current.templateUrl;
$templateCache.remove(currentPageTemplate);
$route.reload();

您还可以使用离子刷新器在页面上创建下拉刷新操作

http://ionicframework.com/docs/v2/api/components/refresher/Refresher/

试试这个代码:

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

如果您愿意遵循通用惯例,我发现了一种重新加载当前视图(包括其所有参数)的非常简单的方法。我使用 Ionic3 对此进行了测试,但它仍应适用于 Ionic 2

将每个页面的所有初始化代码移动到 ionViewDidLoad(),这是 运行 第一次加载视图时的一次

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Api } from '../../providers/api';
import { Movie } from '../../interfaces/movie';

@Component({
    selector: 'page-movie-info',
    templateUrl: 'movie-info.html'
})
export class MovieInfoPage {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public api: Api
    ) {
    }

    /**
     * Run all page initialization in this method so that this page
     * can be refreshed simply by re-calling this function
     */
    ionViewDidLoad() {
        //access any parameters provided to the page through navParams. 
        var movieId = this.navParams.data.movieId;
        this.api.movies.getById(movieId).then((movie) => {
            this.movie = movie;
        });
    }
    public movie: Movie;
}

您可以从应用程序的其他任何位置使用此代码重新加载当前视图

//get the currently active page component
var component = this.navController.getActive().instance;
//re-run the view load function if the page has one declared
if (component.ionViewDidLoad) {
    component.ionViewDidLoad();
}

我会这样做:(基于@Ahmad Aghazadeh 的回答)

this.navCtrl.push(this.navCtrl.getActive().component).then(() => {
   let index = this.viewCtrl.index;
   this.navCtrl.remove(index);
})

=>再次推送此页面(重新加载)

=> 删除我们所在的页面(使用索引)

Html:

  <ion-refresher (ionRefresh)="doRefresh($event)">
    <ion-refresher-content></ion-refresher-content>
  </ion-refresher>

</ion-content>

打字稿:

@Component({...})
export class NewsFeedPage {

  doRefresh(refresher) {
    console.log('Begin async operation', refresher);

    setTimeout(() => {
      console.log('Async operation has ended');
      refresher.complete();
    }, 2000);
  }

}

来源:Ionic doc

在 ionic 3 的异步函数中使用 ion-refresher 的示例:

在您的 .html 文件中:

<ion-content no-padding >
<ion-refresher (ionRefresh)="doRefresh($event)">
    <ion-refresher-content></ion-refresher-content>
  </ion-refresher>

并在您的 .ts 文件中:

    constructor(...) {
     ...
    samplefuncion(null){
       asyncFunction().then(()=>{
    ...//after success call
    ...
    if (event)    
    event.complete();

    },(error)=>{
    ....
    if (event)
    event.complete();
    })
    }

         doRefresh(event) {
            samplefuncion(event);        
          }

试试这个,只弹出一个页面,然后再次推送该页面。

this.navCtrl.pop(); 
this.navCtrl.push(NewPage);

希望这会有所帮助。