0

I am trying to send data(Object) from one component to another however I am getting an empty result.

Here is my code

routing module

{ path: 'booking/details', component: DetailsComponent, data: { title: extract('Details'), dataTransfer: '' } },

sending component

this.router.navigate(['/booking/details', this.caseData]);

this.caseData looks like this

{
  "getAllInfo": {
    "ticket": {
      "internalNum": "12345",
      "comp": "11"
    },
    "caseInfo": {
      "masVolgnommer": "1",
      "masMaand": "1",
      "masJaar": "2010"
    }
  }
}

receiving component

this.caseData = this.route
 .data
 .subscribe(v => console.log(v.dataTransfer));
2
  • 1
    The values in that array are used to build the URL, not the data property. It sounds like you may be better off creating a shared service. Commented Nov 8, 2018 at 6:31
  • yes you are correct, I am just passing data from one component to another so doing it through the url is not correct Commented Nov 8, 2018 at 6:34

2 Answers 2

3

Data property is not needed in your route - without adding data you can bind data as a json and read it { path: 'booking/details', component: DetailsComponent } this is fine to pass data while routing - whereas data property in your route declaration is used to pass data every time when the route is navigated

When you try to navigate booking/details everytime you will get data {title: "Details", dataTransfer: ""} to read this data you can inject ActivatedRouteSnapshotin your constructor and read as

this.activatedRouteSnapshot.data["title"]this will return Details

In your case if you want to pass data to another component just pass the data as a Json

this.router.navigate(['/booking/details', { caseData : this.caseData }]);

Finally you can read the data in the same way mentioned above - this.activatedRouteSnapshot.data["caseData"]

Hope this works - Happy coding !!

Sign up to request clarification or add additional context in comments.

Comments

0

set the caseData inside the subscribe method

this.route
 .data
 .subscribe(v => {
   this.caseData = v;
   console.log(this.caseData) 
});

1 Comment

this is the response {title: "Details", dataTransfer: ""} The data is not getting inserted into the dataTransfer parameter

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.