In this section, we will explore how to create and use pages in an Ionic application. Pages are fundamental building blocks in Ionic apps, allowing you to structure your app into manageable and navigable sections.

Key Concepts

  1. Pages in Ionic: Pages are individual views or screens in an Ionic application.
  2. Generating Pages: Using the Ionic CLI to generate new pages.
  3. Navigation: Navigating between pages using Angular's Router.
  4. Passing Data: Passing data between pages.

Generating a New Page

Ionic CLI provides a simple command to generate new pages. This command creates the necessary files and updates the routing configuration.

Step-by-Step Guide

  1. Open your terminal and navigate to your Ionic project directory.

  2. Run the following command to generate a new page:

    ionic generate page PageName
    

    Replace PageName with the desired name of your page. For example, to create a page named Details, you would run:

    ionic generate page Details
    
  3. Ionic CLI Output: The CLI will create a new folder named details in the src/app directory with the following files:

    • details.page.html: The HTML template for the page.
    • details.page.scss: The SCSS file for styling the page.
    • details.page.ts: The TypeScript file for the page logic.
    • details.page.spec.ts: The spec file for unit testing the page.

Understanding the Generated Files

details.page.html

This file contains the HTML template for the page. You can define the structure and content of your page here.

<ion-header>
  <ion-toolbar>
    <ion-title>Details</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <p>Details Page Content</p>
</ion-content>

details.page.scss

This file contains the SCSS styles for the page. You can add custom styles specific to this page.

page-details {
  ion-content {
    p {
      font-size: 20px;
      color: #333;
    }
  }
}

details.page.ts

This file contains the TypeScript logic for the page. It includes the necessary imports and the component decorator.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-details',
  templateUrl: './details.page.html',
  styleUrls: ['./details.page.scss'],
})
export class DetailsPage implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

Navigation Between Pages

To navigate between pages, Ionic uses Angular's Router. You can define routes in the app-routing.module.ts file.

Adding a Route

  1. Open src/app/app-routing.module.ts.

  2. Add a new route for the DetailsPage:

    const routes: Routes = [
      {
        path: '',
        loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
      },
      {
        path: 'details',
        loadChildren: () => import('./details/details.module').then(m => m.DetailsPageModule)
      }
    ];
    

Navigating Programmatically

You can navigate to the DetailsPage programmatically using Angular's Router.

  1. Import the Router in your component:

    import { Router } from '@angular/router';
    
  2. Inject the Router in the constructor:

    constructor(private router: Router) { }
    
  3. Use the navigate method to navigate to the DetailsPage:

    this.router.navigate(['/details']);
    

Navigating Using Ionic Components

You can also use Ionic components like ion-button to navigate between pages.

<ion-button [routerLink]="['/details']">Go to Details</ion-button>

Passing Data Between Pages

To pass data between pages, you can use Angular's NavigationExtras.

Example

  1. Navigate with Data:

    this.router.navigate(['/details'], {
      queryParams: { id: 123 }
    });
    
  2. Retrieve Data in the Target Page:

    import { ActivatedRoute } from '@angular/router';
    
    constructor(private route: ActivatedRoute) { }
    
    ngOnInit() {
      this.route.queryParams.subscribe(params => {
        const id = params['id'];
        console.log(id); // 123
      });
    }
    

Practical Exercise

Task

  1. Generate a new page named Profile.
  2. Add a route for the ProfilePage in app-routing.module.ts.
  3. Create a button in the HomePage that navigates to the ProfilePage.
  4. Pass a username from the HomePage to the ProfilePage and display it.

Solution

  1. Generate the ProfilePage:

    ionic generate page Profile
    
  2. Add the route in app-routing.module.ts:

    const routes: Routes = [
      {
        path: '',
        loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
      },
      {
        path: 'profile',
        loadChildren: () => import('./profile/profile.module').then(m => m.ProfilePageModule)
      }
    ];
    
  3. Add a button in home.page.html:

    <ion-button [routerLink]="['/profile']" [queryParams]="{ username: 'JohnDoe' }">Go to Profile</ion-button>
    
  4. Retrieve and display the username in profile.page.ts:

    import { ActivatedRoute } from '@angular/router';
    
    constructor(private route: ActivatedRoute) { }
    
    ngOnInit() {
      this.route.queryParams.subscribe(params => {
        const username = params['username'];
        console.log(username); // JohnDoe
      });
    }
    

    Display the username in profile.page.html:

    <ion-header>
      <ion-toolbar>
        <ion-title>Profile</ion-title>
      </ion-toolbar>
    </ion-header>
    
    <ion-content>
      <p>Welcome, {{ username }}</p>
    </ion-content>
    

Summary

In this section, we learned how to create and use pages in an Ionic application. We covered generating new pages using the Ionic CLI, navigating between pages using Angular's Router, and passing data between pages. These concepts are fundamental for building structured and navigable Ionic applications. In the next section, we will explore Ionic components in more detail.

© Copyright 2024. All rights reserved