Page constructor being executed multiple times
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 I can't reproduce this issue
@manucorporat This only occurs on device or in the emulator (iOS), not in serve.
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()
Okay, I got a build for android going, and this issue seems to be iOS only.
I have the same problem on browser when running ionic serve
@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 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?
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
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.