Refresh a page in ionic2

前端 未结 8 1377
灰色年华
灰色年华 2020-12-16 00:34

Is there a way to refresh only a page i.e. only one screen in ionic2.

I tried :

window.location.reload();

and

loca         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 01:04

    If you are willing to follow a common convention, I have found a very easy way to reload the current view (including all of its parameters). I tested this using Ionic3, but it should still apply in Ionic 2

    Move all of your initialization code for every page into ionViewDidLoad(), which is run ONCE on the first time the view is loaded

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

    From anywhere else in the app, you can reload the current view with this code

    //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();
    }
    

提交回复
热议问题