2

I would like to pass objects during router navigate to the target component. Currently I am using routeParams and I stringify my objects to strings. This approach works, but I don't like this solution. How would a better solution look like?

export class Overview {
    //import {Router} from "@angular/router-deprecated";
    constructor(private router:Router) {}
    goToElementComponent(elem:Element) {
        this.router.navigate(['ElementDetail', {elem: JSON.stringify(elem)}]);
    }
}
export class ElementDetail {
    // import {RouteParams, Router} from "@angular/router-deprecated";
    this.elem : Element;

    constructor(private routeParams:RouteParams) {}

    ngOnInit() {
        var elem = JSON.parse(this.routeParams.get("elem"));
        if (elem) {
          this.elem = elem;
        } else {
          this.elem = new Element();
        }
    }
} 
2
  • With the next update (RC.4) data will be supported again. See also stackoverflow.com/questions/37077094/… Commented Jun 30, 2016 at 13:26
  • Thanks. I will wait for RC.4 and then update this question. Commented Jun 30, 2016 at 13:50

4 Answers 4

9
//Note: router:Router and route:ActivatedRoute
//send object from one component (its independent parent-child relationship with //another component): 
  bill = {
    'customerName': "RAJESH",
    'billNo': "A230",
    'date': "23/12/2020",
    'address': "AGRA",
    'itemDetails':"HUSQVANA VITIPILEN",
    'grandTotal': 2500000
  }
this.router.navigate(['/billingpdf',{billing:JSON.stringify(this.bill)}])

//accessing this object into another component like this:
//-------------------------------------------------------
this.route.snapshot.paramMap.get('billing') //store this variable if you want use further
                                    or
this.route.snapshot.paramMap.getAll('billing')
Sign up to request clarification or add additional context in comments.

Comments

3

Another approach would be to store the temporary params in a Shared Service

You'd have to inject the service into constructors, in order to write/read your object params

2 Comments

But do not put it in the providers (if you do this, it will create a new object and all data will be lost).. . You also could use an EventEmitter to pass/emit the data and subscribe to it in the target.. .
I have included the ElementDataService into main.ts bootstrap. Besides the service just consist of one property and getter and setter methods. It works currently for me. How can it be done better?
1

You can do it using NavigationExtras parameter of navigate method. NavigationExtras object should have state parameter where you can put your custom data:

this.router.navigate(['/route-to'], {
      state: {
        someData: { name: 'Some name', description: 'Some description' },
      },
    });

In targeted component we receive data this way:

constructor(private router: Router) {
 this.dataFromRoute = this.router.getCurrentNavigation().extras.state?.['someData'];
}

Comments

-1

Pass object in the queryParams like the following:

this.router.navigate(['/my-route'], {queryParams: myObject});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.