How to navigate between pages using buttons with Ionic?

Asked

Viewed 5,065 times

1

I’m trying to understand how to navigate between pages with ionic2. I want that by clicking the button below it redirect me to HomePage, how can I do this?

Button in.html menu

<button ion-button color="light">Button</button>

My . ts

@Component({
  selector: 'page-menu',
  templateUrl: 'menu.html',  

})
export class MenuPage { 

  constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController) {
  }

  showAlert() {
    const alert = this.alertCtrl.create({
      title: 'New Friend!',
      subTitle: 'teste',
      buttons: ['OK']
    });
    alert.present();
  }

  openSobre(){
    this.navCtrl.push(HomePage, {}, {animate: true} );    
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MenuPage');
  }

}

1 answer

1


Let’s assume you want to go from Homepage to Sobrepage by clicking a button that is on the Homepage.

Within Homepage.ts import the NavController and the component SobrePage. Create a function openSobre(), which will be called when clicking the button.

import { Component } from '@angular/core';
import { NavController} from 'ionic-angular';
import { SobrePage } from "../sobre/sobre";

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

  constructor(public navCtrl: NavController) { }  

  openSobre(){
    this.navCtrl.push(SobrePage, {}, {animate: true} );    
  }
}

On your button, call the function openSobre().

<button ion-button (click)="openSobre()" color="light">Button</button>

Further details can be found on documentation

  • I have my Menupage that is as root and I want to go to Homepage, in which . ts I put?

  • Your menu page is a page or side menu?

  • a page I put as root page

  • Got it. So through it you will call the Homepage page.

  • my Component already has @Component(' selector: 'page-home', templateUrl: home.html'}) , just add the selector: 'page-home', templateUrl: 'home.html in it?

  • Edit your question by asking how your Menupage.ts is doing

  • added, take a look

  • You already have the Homepage component created with the . ts and . html files?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.