Ionic Navigation and Routing

Facebooktwitterredditpinterestlinkedinmailby feather

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:

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

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

Home.page.html

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

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

Home.page.ts

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

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

Login.page.ts

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.

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

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

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

Home.page.ts

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

Profile.component.html

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
Facebooktwitterredditpinterestlinkedinmailby feather