Ionic 4. Alternative to NavParams

后端 未结 8 1642
灰色年华
灰色年华 2021-01-06 10:55

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) {
            


        
相关标签:
8条回答
  • 2021-01-06 11:37

    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();
      }
    
    0 讨论(0)
  • 2021-01-06 11:39

    Very very simply, you can do something like this:

    In the "calling screen" :

    this.router.navigate(['url', {model: JSON.stringify(myCustomObject)}])
    

    In the "called screen" :

    this.myvar = JSON.parse(this.activatedRoute.snapshot.paramMap.get('model')
    

    Et voilà !

    0 讨论(0)
提交回复
热议问题