How to navigate between pages in ionic 4 & 5?
Asked Answered
F

1

5

I had a project that I developed with ionic 3. But I took a break and when I started working again with ionic, I saw the navigation system change in the new versions. My project is a simple project. This project that lists the data in the a array and details about the data appear on a different page.

I was doing this on Ionic 3: homepage.ts

 export class HomePage  {

  items = [];

    constructor(public navCtrl: NavController) {
      this.initializeItems();}
      initializeItems() {
      this.items = [
   {
    'title': 'John',
    'image': '',
    'hair': 'black',
  },
   {
    'title': 'Brendon',
    'image': '',
    'hair': 'blonde',
  }];

openNavDetailsPage(item) {
    this.navCtrl.push(DetailsPage, { item: item });
  }

detailspage.ts

 export class DetailsPage {
   item;

   constructor(params: NavParams) {
     this.item = params.data.item;
   }
 }

NavCtrl and NavParams are no longer available in version 5 (and I think in version 4). I did to navigate from the home page to the next page(ionic 5). homepage.ts:

    toDetailsPage(){
      this.router.navigate(['details'])
    }

However, I couldn't navigate according to the data on my list. How can I do this according to the next generation version?

Fariss answered 20/8, 2019 at 11:52 Comment(0)
F
9

app.routing.module.ts (Routing Module)

const itemRoutes: Routes = [
    { path: 'item', component: ItemListComponent},
    { path: 'DetailsPage/:id', component: DetailComponent  }
];

homepage.ts file

import { Router } from '@angular/router';

  export class HomePage  {
    
          constructor(private router: Router)
    
          openNavDetailsPage(item) {
            this.router.navigate(['/DetailsPage', { item: item }]);
          }
     }

.html file

If you directly want to route through html page:

<ion-button routerLink="/DetailsPage">Log In </ion-button>

or

<ion-button [routerLink]="['/DetailsPage', item.id]">Log In </ion-button>

detail.ts file

import { ActivatedRoute, Router } from '@angular/router';

export class DetailComponent implements OnInit {

  id: any;

  constructor(private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
     this.id = this.route.snapshot.paramMap.get('id');
   }
}

In addition to using a JSON

homepage.ts file

this.router.navigate(['/DetailsPage', { item: JSON.stringify(item) }]);

detail.ts file

this.item = JSON.parse(this.route.snapshot.paramMap.get('item'));

one more way

homepage.html

<div *ngFor=""let item of items"> // here items is an array
   <button (click)="goToDetail(item)" class="rgt-btn">
        <ion-icon slot="icon-only" name="add-circle" ></ion-icon>
   </button>
</div>

homepage.ts

   import { NavigationExtras, Router } from '@angular/router';
    
      export class HomePage  {
    
        constructor(private router: Router)
    
        goToDetail(item){
           let navigationExtras: NavigationExtras = item; 
           this.router.navigate(['/DetailsPage'], navigationExtras);
         }
     }

DetailsPage.ts

import { Router } from '@angular/router';

export class DetailComponent implements OnInit {

  item: any;

  constructor(private router: Router) {
    if (this.router.getCurrentNavigation()) {
        this.item = this.router.getCurrentNavigation().extras;
  }
 }
    

}

> OR With the help of service page

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class NavExtrasService {

    extras: any;

      constructor() { }

      public setExtras(data){
        this.extras = data;
      }

      public getExtras(){
        return this.extras;
      }
    }

Let's say I'm navigating from home to detail page, In page A:

this.navExtras.setExtras(extras)
this.router.navigateByUrl('detailPage');

Then in Detail Page, I retrieve the extras this way:

 let newData: any;
 this.newData = navExtras.getExtras();
Frantz answered 20/8, 2019 at 13:45 Comment(8)
Our datas is in homepage.ts. Should I create an ItemList page and add it in to the router? Or should I create a separate component within the homepage?Fariss
create an ItemList page and add it in to the routerFrantz
How can I connect ItemList and home and Details? I created ItemListPage and added my datas in that page.Fariss
just create service(provider) page and do functionality there...with the help of service page u can get dataFrantz
You can also visit joshmorony.com/using-angular-routing-with-ionic-4Frantz
This worked! Thank you. In this case, I am able to send more than one param just by using this.route.snapshot.paramMap.get('otherparam');Knuckleduster
Question is how to navigate between ionic pages and you answered of navigate between component.Trimble
@Rohit Tagadiya It is navigating between 2 pages. With the help of router you can navigate.Frantz

© 2022 - 2024 — McMap. All rights reserved.