In this section, we will explore how to navigate between different pages in an Ionic application and how to set up routing to manage the navigation flow. Understanding navigation and routing is crucial for building dynamic and user-friendly applications.
Key Concepts
- Navigation: The process of moving between different pages or views in an application.
- Routing: The mechanism that maps URLs to specific pages or components in the application.
Setting Up Navigation and Routing
- Creating Pages
First, let's create a couple of pages to navigate between. Use the Ionic CLI to generate new pages:
This will create two new pages: HomePage
and AboutPage
.
- Configuring Routes
Next, we need to configure the routes in our application. Open the src/app/app-routing.module.ts
file and define the routes:
import { NgModule } from '@angular/core'; import { PreloadAllModules, RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', loadChildren: () => import('./home/home.module').then(m => m.HomePageModule) }, { path: 'about', loadChildren: () => import('./about/about.module').then(m => m.AboutPageModule) } ]; @NgModule({ imports: [ RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }) ], exports: [RouterModule] }) export class AppRoutingModule { }
- Navigating Between Pages
To navigate between pages, we use the NavController
service provided by Ionic. Inject NavController
into your component and use its navigateForward
method to navigate to a different page.
Example: Navigating from Home to About
In src/app/home/home.page.ts
:
import { Component } from '@angular/core'; import { NavController } from '@ionic/angular'; @Component({ selector: 'app-home', templateUrl: './home.page.html', styleUrls: ['./home.page.scss'], }) export class HomePage { constructor(private navCtrl: NavController) { } goToAbout() { this.navCtrl.navigateForward('/about'); } }
In src/app/home/home.page.html
:
<ion-header> <ion-toolbar> <ion-title> Home </ion-title> </ion-toolbar> </ion-header> <ion-content> <ion-button (click)="goToAbout()">Go to About Page</ion-button> </ion-content>
- Using Router Links
Alternatively, you can use Angular's routerLink
directive to navigate between pages without writing any TypeScript code.
In src/app/home/home.page.html
:
<ion-header> <ion-toolbar> <ion-title> Home </ion-title> </ion-toolbar> </ion-header> <ion-content> <ion-button routerLink="/about">Go to About Page</ion-button> </ion-content>
- Passing Data Between Pages
You can pass data between pages using the state
property of the NavController
.
Example: Passing Data from Home to About
In src/app/home/home.page.ts
:
goToAbout() { this.navCtrl.navigateForward('/about', { state: { data: 'Hello from Home Page' } }); }
In src/app/about/about.page.ts
:
import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'app-about', templateUrl: './about.page.html', styleUrls: ['./about.page.scss'], }) export class AboutPage implements OnInit { data: any; constructor(private router: Router) { if (this.router.getCurrentNavigation().extras.state) { this.data = this.router.getCurrentNavigation().extras.state.data; } } ngOnInit() { } }
In src/app/about/about.page.html
:
<ion-header> <ion-toolbar> <ion-title> About </ion-title> </ion-toolbar> </ion-header> <ion-content> <p>{{ data }}</p> </ion-content>
Practical Exercise
Task
- Create a new page called
Contact
. - Add a button on the
Home
page that navigates to theContact
page. - Pass a message from the
Home
page to theContact
page and display it on theContact
page.
Solution
- Generate the
Contact
page:
- Add the route for the
Contact
page inapp-routing.module.ts
:
{ path: 'contact', loadChildren: () => import('./contact/contact.module').then(m => m.ContactPageModule) }
- Update
home.page.ts
to navigate to theContact
page with a message:
goToContact() { this.navCtrl.navigateForward('/contact', { state: { message: 'Hello from Home Page' } }); }
- Update
home.page.html
to add the button:
- Update
contact.page.ts
to receive and display the message:
import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'app-contact', templateUrl: './contact.page.html', styleUrls: ['./contact.page.scss'], }) export class ContactPage implements OnInit { message: string; constructor(private router: Router) { if (this.router.getCurrentNavigation().extras.state) { this.message = this.router.getCurrentNavigation().extras.state.message; } } ngOnInit() { } }
- Update
contact.page.html
to display the message:
<ion-header> <ion-toolbar> <ion-title> Contact </ion-title> </ion-toolbar> </ion-header> <ion-content> <p>{{ message }}</p> </ion-content>
Summary
In this section, we covered the basics of navigation and routing in Ionic. We learned how to create pages, configure routes, navigate between pages using NavController
, and pass data between pages. These concepts are fundamental for building a dynamic and interactive Ionic application. In the next section, we will explore tabs and side menus to enhance the navigation experience further.
Ionic Development Course
Module 1: Introduction to Ionic
- What is Ionic?
- Setting Up the Development Environment
- Creating Your First Ionic App
- Understanding the Project Structure
- Running and Debugging Your App
Module 2: Basic Components and Navigation
- Ionic Components Overview
- Using Ionic Buttons and Icons
- Creating and Using Pages
- Navigation and Routing
- Tabs and Side Menus
Module 3: Styling and Theming
- Introduction to Ionic Styling
- Using CSS and SCSS in Ionic
- Theming Your Ionic App
- Responsive Design in Ionic
- Customizing Ionic Components
Module 4: Working with Data
- Introduction to Data Binding
- Using Angular Services
- HTTP Requests and APIs
- Storing Data Locally
- Using Ionic Storage
Module 5: Advanced Components and Features
- Using Ionic Forms
- Validation and Error Handling
- Using Ionic Native and Cordova Plugins
- Accessing Device Features
- Push Notifications
Module 6: Testing and Deployment
- Unit Testing in Ionic
- End-to-End Testing
- Building for Production
- Deploying to App Stores
- Continuous Integration and Delivery