如果你愿意遵循一个共同的约定,我发现了一种非常简单的方法来重新加载当前视图(包括它的所有参数)。我使用 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();
}