×

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 progress bar

The ionic progress bar element is a horizontal progress bar used to visualize the progression of operation and activity. You can use the <ion-progress-bar> component to create a progress bar in the ionic...

4 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.

Ionic vs Apache Cordova

Ionic Vs Apache Cordova Ionic and Cordova are two popular technology going on in the market for mobile application development. The combination of these two technologies creates a development platform that is known as...

3 minutes read.

Ionic framework Installation

Installation of Ionic Before proceeding with the ionic application, you'll need to install node.js and NPM (Node Package Manager), the code editor, and Ionic CLI. Installation of node.js and NPM These are the primary...

5 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.

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 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 vs React Native

Ionic Vs React Native Ionic and React Native are both common framework which is used to develop mobile applications. Here, you will know about the popular differences between the ionic and the React Native. Ionic Ionic...

3 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 Toolbar

A toolbar is a general toolbar that can be used in an application such as a header, sub-header, footer, or even a sub-footer. Because the ion-toolbar is flexbox-dependent, no matter how many toolbars...

3 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 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 Spinner

The ionic spinner component provides the various SVG (Scalable Vector Graphic) spinners. Ionic spinners are the visual indicators that display the app which is loading content or performing another process that the user...

5 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.

Features of Ionic Framework

The Ionic Framework is based on the AngularJS framework that permits a programmer to use a combination of many other programming languages, like HTML5, CSS, and JavaScript. Some of the...

4 minutes read.

Ionic vs Xamarin

Ionic vs Xamarin Ionic and Xamarin are both the most popular frameworks used to develop a hybrid mobile application. Both frameworks are used in large enterprises by the developers. Here, we will discuss the...

4 minutes read.

Ionic Buttons

Ionic Buttons There are different types of buttons in the Ionic framework. These buttons are subtly animated, enhancing the user experience when using the application. Buttons are a fundamental way of interacting and navigating...

4 minutes read.

Ionic Actionsheet

Ionic Actionsheet The Action sheet is a dialog box that displays a group of options. It is shown on the top of the application content and must be dismissed manually before the user can resume...

3 minutes read.

Ionic Reorder

The Ionic offers the <ion-reorder> component to reordering features. Ionic reorder component adds the Drag and Drop feature to list items, in which the user can change the order of the items. Reorder...

3 minutes read.

Ionic List

Ionic List The ionic list is created of several rows of items that can contain text, buttons, thumbnails, keys, icons, and more. Lists are one of the essential components in any web or...

4 minutes read.