Hacker
Hacker

Reputation: 7906

Pass json data while routing to another component in angular 4

I have a form and when user submits the form. I make a service all and get the status of the form submission. It can be error/success/exact message also. As soon as i get the response from back end, i route to new component. How do i pass the json response to new component where in it can decide what message to show i.e. error or success. One method is service. Is there any other way in angular 4 for this.

Upvotes: 1

Views: 692

Answers (1)

joshsisley
joshsisley

Reputation: 310

Unfortunately, I believe that the best answer is to just use a shared service between the two components.

The reason why the service is the best is because the router needs to serialize the data to the browsers URL bar, and the URL bar only supports a string. Therefore you wouldn't be able to pass an object through the router.

One other option would be to use localStorage and save the object that way and then just get the stored object when the next component loads. But I would probably recommend the shared service over localStorage.

Upvotes: 1

Related Questions