Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4. Alternative to NavParams

I am using ionic 4. It does not accept to receive data using navparams. Here is my sender page method:

  //private route:Router
  gotoFinalView(intent) {
    this.route.navigateByUrl(`${intent}`, this.destination);
  }

Receiver page line;

  //private navParams:NavParams  
  this.destination = navParams.data;

What is the right approach to doing this in ionic 4. I am also uncertain whether gotoFinalView method is valid.

like image 503
7guyo Avatar asked Jan 02 '23 13:01

7guyo


1 Answers

This is how I solved my problem:

I created a Service with a setter and getter methods as;

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

@Injectable({
  providedIn: "root"
})
export class MasterDetailService {
  private destn: any;
  constructor() {}

  public setDestn(destn) {
    this.destn = destn;
  }

  getDestn() {
    return this.destn;
  }
}

Injected the Service and NavController in the first page and used it as;

  gotoFinalView(destn) {
    this.masterDetailService.setDestn(destn);
    this.navCtrl.navigateForward("destn-page");
  }

Extracted the data at the final page by;

constructor(
    private masterDetailService: MasterDetailService
  ) {
    this.destination = this.masterDetailService.getDestn();
  }
like image 66
7guyo Avatar answered Jan 05 '23 14:01

7guyo