ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Page constructor being executed multiple times

Open Tyler-Darby opened this issue 10 years ago • 9 comments

Ionic version: [ ] 1.x [x ] 2.x

I'm submitting a ... [ x] bug report [ ] feature request [ ] support request Current behavior: Upon navigating to a page with setRoot(), the constructor function, as well as ionViewDidLoad() is being called 10 times.

Expected behavior: These functions should only be called once.

Steps to reproduce: Create two pages, one with a button to the other using this.nav.setRoot(Page2). Clicking this will cause the constructor to be called 10 times.

Related code: Button page: Template

<ion-content padding scroll="false">
    <div id="buttons">
        <div style="text-align: center;">
          <button ion-button color="white" outline (click)="changePage()">Change the page</button>
        </div>
    </div>
</ion-content>

TypeScript

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Main } from '../main/main';

@Component({
  selector: 'page-button',
  templateUrl: 'button.html'
})
export class ButtonPage {

  constructor(public navCtrl: NavController) {}

  changePage(){
    this.navCtrl.setRoot(Main);
  }

}

Main Page:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-main',
  templateUrl: 'main.html'
})
export class Main {

  constructor(public navCtrl: NavController) {
     console.log("Constructor Test")
    // this will be run multiple times
  }

}

Ionic info:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.3-201611261840
Ionic CLI Version: 2.1.13
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.46
ios-deploy version: 1.9.0 
ios-sim version: 5.0.6 
OS: macOS Sierra
Node Version: v6.3.1
Xcode version: Xcode 8.1 Build version 8B62

Tyler-Darby avatar Nov 28 '16 22:11 Tyler-Darby

@Tyler-Darby I can't reproduce this issue

manucorporat avatar Nov 29 '16 12:11 manucorporat

@manucorporat This only occurs on device or in the emulator (iOS), not in serve.

Tyler-Darby avatar Nov 29 '16 15:11 Tyler-Darby

It also appears that not only is the constructor being executed multiple times, but the entire page is being rendered 10 times.

Additionally, when navigating with .push(), it works fine. Only bugs out with .setRoot()

Tyler-Darby avatar Nov 29 '16 15:11 Tyler-Darby

Okay, I got a build for android going, and this issue seems to be iOS only.

Tyler-Darby avatar Nov 29 '16 17:11 Tyler-Darby

I have the same problem on browser when running ionic serve

pantonis avatar Nov 29 '16 18:11 pantonis

@pantonis I managed to fix this. Somehow I wasn't thinking and called IonicModule.forRoot() in imports in app.module.ts for every page in my app not just the main app class itself, just a dumb mistake on my part. Could this be your issue?

Tyler-Darby avatar Nov 30 '16 18:11 Tyler-Darby

@Tyler-Darby could you explain your fix more verbosely? It would seem the standard starter menu template comes with IonicModule.forRoot() in imports in app.module.ts and I'm getting the same behaviour in all devices/web, each page entry results in ionviewdidload being called.

I'm not sure what you did to stop this behaviour??? Removing the IonicModule.forRoot() from imports just makes the app not work?

andmar8 avatar Feb 13 '17 14:02 andmar8

Nevermind, in my case it appears to be the use of setRoot resetting the page every time you enter it, see....

https://github.com/driftyco/ionic/issues/5960

https://forum.ionicframework.com/t/after-setroot-tabspage-the-default-tab-page-appears-twice/71770/9

andmar8 avatar Feb 13 '17 14:02 andmar8

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 04 '18 02:09 ionitron-bot[bot]