Geoff
Geoff

Reputation: 6629

Angular pass data in the route

I am passing data this way

console.log(this.regForm.value); //there is data
    this._router.navigate(["/tsafety/checklist-checks"], this.regForm.value);

In the component with routing checklist-checks am trying to retrieve the data using

constructor(private route:ActivatedRoute)
{
  this.route
   .params
    .subscribe(params => {
      this.truckdetails = params; //this is never set
      console.log(params);// this is empty
     });
}

Whenever I am trying to accessing this.truckdetails it is always empty.

What else do I need to add to get the passed data?

Upvotes: 0

Views: 238

Answers (3)

Johnny Tung
Johnny Tung

Reputation: 11

I use switchMap in my project.

this.route.params
  .switchMap((params: Params) => {
   console.log(params);
});

Upvotes: 0

JeanPaul A.
JeanPaul A.

Reputation: 3724

The navigation is incorrect. Optional params (like the form data that you are passing) should reside inside the square brackets. The second argument of the navigate() function takes extra information regarding the navigation. See : https://angular.io/api/router/Router#navigate

Your code should be

this._router.navigate(["/tsafety/checklist-checks", this.regForm.value]);

Upvotes: 1

agriboz
agriboz

Reputation: 4854

You should try accessing data using this.route.snapshot.data['yourdata']

Upvotes: 1

Related Questions