×

Ionic Navigation and Routing

The latest version (ionic 4) of the ionic framework, no longer limited to the angular. In Ionic 4, you can create an ionic project in different front-end frameworks such as Vue.js, React.js, and Angular. The angular application contains an important library, such as an angular router, for navigating on different pages. The application will be able to maintain its navigation state if the router is available. You can create a rich application with the angular router and the ionic framework. In this section, you will learn about the routing that is based on an angular.

Navigation is essential for user experience in the development field. By using the navigation, you can move from one page to another page. An important part of the ionic application development is configuring routing and ensuring smooth navigation between the pages.

The Ionic 4 uses a navigation stack for executing navigation between the pages. It uses two stack controllers in the ionic framework to navigate between the pages, i.e., Push and Pop. If you want to go for a new page, ionic push the page on top of the stack, and when you go back to the previous page, ionic will pop page from the stack controller.

Now, we are going to see all the steps of angular routing and navigation and also know how you can navigate between the different pages in the ionic application. Here, we are going to use the various cases to navigate between the pages, as shown below:

Case 1

Step 1: Firstly, you create a new project in the ionic application. It also makes sure that you have installed the latest version of the ionic.

Step 2: After the installation of the latest version of the ionic framework, you need to select a blank template from the various templates. To create new pages, run the following command:

$ ionic g pages
login

Step 3: In this step, you open the app-routing.module.ts file and set the path in the file, as shown below:

App-routing.module.ts

import { NgModule
} from '@angular/core';
 import { Routes, RouterModule } from '@angular/router';
 const routes: Routes = [
   { path: '', redirectTo: 'home', pathMatch: 'full' },
   { path: 'home', loadChildren: './home/home.module#HomePageModule' },
   { path: 'login', loadChildren: './login/login.module#LoginPageModule' },
   { path: 'login', loadChildren: './login/login.module#LoginPageModule' }
 ];
 @NgModule({ 
   imports: [RouterModule.forRoot(routes)],
   exports: [RouterModule]
 })
 export class AppRoutingModule { } 

Step 4: After creating the page, you are required to open the home.page.html file and write the following code:

Home.page.html

<ion-header>  
   <ion-toolbar color="primary">  
     <ion-title>  
       Ionic Navigation Example 
     </ion-title>  
   </ion-toolbar>  
 </ion-header>   
 <ion-content>  
   <div class="padding">  
     <p>Hello World! </p>  
     <ion-button color="danger" href="/login">Login</ion-button>  
   </div>  
 </ion-content>   

Step 5: After executing this code, you will get the output, as shown below:

Ionic Navigation and Routing

Step 6: When you click on the Login button, it will show the following output:

Ionic Navigation and Routing

Case 2

In this case, you can navigate between the different pages by using programming in the ionic application, as shown in the below example.

Step 1: In this step, open the home.page.html file and write the code, as shown below:

Home.page.html

<ion-header>  
   <ion-toolbar color="primary">  
     <ion-title>  
       Ionic Navigation Example 
     </ion-title>   
   </ion-toolbar>  
 </ion-header>  
 <ion-content>   
   <div class="padding">  
     <p> Hello World! </p>   
     <ion-button color="danger" (click)="login()">Login</ion-button> 
   </div>  
 </ion-content>   

Step 2: Next, open the home.page.ts file and write the following code:

Home.page.ts

import {
Component } from '@angular/core';  
 import { Router } from '@angular/router';  
 @Component({  
   selector: 'app-home',   
   templateUrl: 'home.page.html',  
   styleUrls: ['home.page.scss'],  
 })   
 export class HomePage {  
   constructor(private router: Router) {}  
   login() {  
     this.router.navigate(['login']);  
   }  
 } 

Step 3: In this step, open the login.page.html file and add the back button to navigate the previous page in the ionic application. For doing this, add the code, as shown below:

Login.page.html

<ion-header>
   <ion-toolbar color="success">
     <ion-title>login</ion-title>
   <ion-buttons slot="start" (click)="goBack()">Back</ion-buttons>
 </ion-toolbar>
 </ion-header>
 <ion-content> 
   <p> This is an ionic navigation example. </p>
 </ion-content> 

Step 4: In this step, open the login.page.ts file and write the following code, as shown below:

Login.page.ts

import {
Component, OnInit } from '@angular/core';
 import { Router } from '@angular/router';
 @Component({
   selector: 'app-login',
   templateUrl: './login.page.html',
   styleUrls: ['./login.page.scss'],
 }) 
 export class LoginPage implements OnInit {
   constructor(private router: Router) { }
   ngOnInit() {
   }
   go(){
   this.router.navigate(['home']);
 }                             
 } 

Step 5: After executing this code, you will get the output, which is shown below:

Ionic Navigation and Routing

Step 6: When you click on the Login button, you will navigate to the login page. After that, when you click on the back button, the page navigates to the previous page, as shown below:

Ionic Navigation and Routing

Case 3

In this case, the user can use the URL to navigate between the pages. Let us understand it with the help of the below example.

Step 1: Here, you create a new component inside the login page. To create a new component, execute the below command.

$ ionic g c
login/profile

Step 2: In this step, open the login.module.ts file and import the ProfileComponent. After that, add the following code in the login.module.ts file, as shown below:

Login.module.ts

import { NgModule
} from '@angular/core';
 import { CommonModule } from '@angular/common';
 import { FormsModule } from '@angular/forms';
 import { Routes, RouterModule } from '@angular/router';
 import { IonicModule } from '@ionic/angular';
 import { LoginPage } from './login.page';
 import { ProfileComponent} from './profile/profile.component'; 
 const routes: Routes = [
   {
     path: '',
     component: LoginPage
   },
   {
   path: 'details',
   component: ProfileComponent
   }
 ]; 
 @NgModule({
   imports: [
     CommonModule,
     FormsModule,
     IonicModule,
     RouterModule.forChild(routes)
   ],
   declarations: [LoginPage, ProfileComponent]
 })
 export class LoginPageModule {} 

Step 3: Now, open the home.page.html file and add a new button. Here, you can pass the random id in the path for routing, as shown below:

 Home.page.html

<ion-header>  
   <ion-toolbar color="primary">  
     <ion-title>  
       Ionic Navigation Example 
     </ion-title>  
   </ion-toolbar>  
 </ion-header>   
 <ion-content>  
   <div class="padding">  
     <p> Hello World! </p>   
     <ion-button color="danger" href="/login/details">Login</ion-button>  
   </div>  
 </ion-content>   

Step 4: Next, open the home.page.ts file and add the following code:

Home.page.ts

import {
Component } from '@angular/core';  
 import { Router } from '@angular/router';  
 @Component({  
   selector: 'app-home',  
   templateUrl: 'home.page.html',  
   styleUrls: ['home.page.scss'],  
 })  
 export class HomePage {  
   constructor(private router: Router) {}  
   login() {  
     this.router.navigateByUrl('/login');  
   }  
 } 

Step 5: In this step, open the profile.component.html file and write the code, as shown below:

Profile.component.html

<ion-content
color="light">
 <p> 
   profile work! <br>
   This is an ionic navigation Example.
 </p>
 </ion-content> 

Step 6: After executing this code, you will get the output, as shown below:

Ionic Navigation and Routing

Step 7: When you click on the login button, the URL navigate to the profile component page, as shown below:

Ionic Navigation and Routing

Related Topics

Ionic Select

The Ion Selection component is similar to the HTML <select> element. However, the Ionic Selection component makes it easy for users to categorize and choose the preferred option or other options. You must...

3 minutes read.

Ionic Range

The Range slider lets users choose from a range of values by moving the slider knob. You can accept two knobs, but by default, one-knob controls the range value. Range labels You can put down on...

2 minutes read.

Difference between Ionic 4 and Ionic 3

The ionic framework is an open-source platform that is used to develop mobile applications. It is also used to build android applications, desktop applications, iOS, and web applications using web technologies like HTML,...

5 minutes read.

Ionic Splash Screen

The Apache Cordova plugin helps to displays and hides a splash screen during the application launch. The Ionic Splash Screen is similar to a home page on a website. The ionic splash...

2 minutes read.

Ionic Navigation and Routing

The latest version (ionic 4) of the ionic framework, no longer limited to the angular. In Ionic 4, you can create an ionic project in different front-end frameworks such as Vue.js, React.js, and...

6 minutes read.

Ionic Date Time

Ionic Date Time The DateTime component is used to display an interface that makes it simple for users to select dates and times. Tapping on <ion-datetime> will display a slider up from the...

4 minutes read.

Ionic Contents

Ionic Contents The content component provides an easy-to-use content area with some useful ways to control the scrollable region. There should be only one content in one view. Content, along with several other Ionic...

2 minutes read.

Ionic-fab Button

Ionic-fab Button The Ionic FABs (Floating Action Buttons) are standard material design compounds. It is a container element that contains one or more fab buttons. They are required to be placed in a fixed position,...

2 minutes read.

Ionic Tabs

The ionic tabs mostly used for mobile navigation. It means that tabs are placed at the top of the display on android devices, while at the bottom on IOS. It does not provide...

2 minutes read.

Ionic Checkbox

Ionic Checkbox Ionic Checkbox is nearly the same as a toggle. It can be placed in an ionic element or used as a separate checkbox. It is designed differently for every platform, such...

2 minutes read.

Ionic Cards

Ionic Cards: Ionic cards are a standard part of the user interface that acts as an entry point for more specific information. These are the best components for displaying information...

4 minutes read.

Ionic Toggle

The toggle is the same as the HTML checkbox entry, but it looks different and easier to use on a touchscreen device. It is an input component that holds the boolean value. Toggle...

2 minutes read.

Ionic Segment

A segment is a collection of buttons, sometimes which are known as segmented controls. The segment buttons are displayed at horizontal rows. These segment buttons can be shown in a toolbar or the...

2 minutes read.

Ionic Modal

Ionic modal is displayed as a temporary UI that slides into the screen. The ionic modal is a dialog box that is displayed at the top of the app’s content, and it...

5 minutes read.

Ionic Input

Ionic Input Inputs are necessary to collect and process user inputs securely. To be easy for users to communicate, they must follow the process and communication guidelines for each platform. It is a...

3 minutes read.

Ionic Editors

Editors are software programs that permit the users to create and edit the text files. In the development field, editors are usually used to create the source code editors that includes many specific...

3 minutes read.

Ionic Infinite Scroll

The infinite scroll permits you to perform an action when the user scrolls a particular content from the bottom or top of the page. When you display the large collection of data...

3 minutes read.

Ionic Camera

Hybrid mobile applications can make very quickly, but sometimes it becomes a challenge to get the features that access the hardware such as GPS, storage, file storage, cameras, and networks. Cordova offers plugins...

4 minutes read.

History of Ionic

It is a complete open-source platform for hybrid mobile application development created by Ben Sperry, Max Lynch, and Adam Bradley of Drifty Co. in 2013. The first alpha version of the framework was...

2 minutes read.

Ionic Grid

The Ionic grid is a very powerful mobile-first flexbox system for building the custom layouts. It is a CSS feature supported by all devices that ionic supports. The Ionic grid is composed of...

11 minutes read.