I am building a game with Angular and Ionic 3 and since the game has a lot of pages/views, I want to eliminate writing the same code in the ts file of the pages I have buttons.
My question is the following, is it possible to create a 'reusable' button that moves the current view to another one? What I mean by 'reusable' is to declare the button click function in app.ts and change the destination page dynamically from the HTML.
What I have done so far:
in home.html
<button ion-button color="light" (click)="moveToPage(gameSettings)">One Player</button>
Note: The page class I want to go to is "gameSetting" which is part of my project in /pages/game-settings/
in app.ts
constructor(public navCtrl: NavController, public navParams: NavParams) {
moveToPage(pageName: String)
{
this.navCtrl.push(pageName);
}
}
Unfortunately, this does not work! What happens is that the HTML passes 'pageName' as a variable with an undefined type i.e. it throws an error. The navCtrl page of Ionic only explains how to move to another page by explicitly the page name in the this.navCtrl.push() function. https://ionicframework.com/docs/api/navigation/NavController/
Any help regarding this behaviour would be much appreciated.