-2

Let's say I have a parent element with router-outlet that routs to children-components. Like in the first 10 seconds of the fireship youtube tutorial: https://youtu.be/Np3ULAMqwNo

I have 10 different but similar child-components. The child component will do some API-requests. How can the parent-component display what went wrong in the API request? I don't want to write the same error-display code 10 times. Should I define some different error-codes and pass them up somehow?

Is it smart to use a router in that case even? What are the alternatives? Make requests in parent and send to child? But then how do I pass a complex response down the router-outlet?

Or can you create a generic "interface" component like in JAVA which has the error-handling and then specify the rest in the implementation of that component? If someone could point me to the right "construct" to do that, please. I researched a bit and found nothing.

I just want to prevent 10x repeated code to show the error in the children.

edit: I am also considering to use ngSwitchCase in parent to select the child-components instead of using routing. Is this maybe the best way to do it here? Binding lots of data between parent/child wouldn't be a problem here, you could probably communicate errors from the child component to the parent component. But how, with what "CONSTRUCT" would this be done the best?

Quick Maths
  • 33
  • 1
  • 7

2 Answers2

1

When you use the router, an injector is created to handle dependency injection.

Thanks to it, you can access all the parents pretty easily.

Here is a demo

export class ChildComponent  {
  constructor(
    private parent: ParentComponent
  ) {
    console.log(parent);
  }
}

This is the cleanest, quickest way to do what you want.

Now, some people will come and say "blablabla this thightly couples your components and prevent reusability".

Well, yes, but 99% of the time people don't look to reuse components. If they do, then they find other ways, such as using a service.

Now, as for the solution you implemented : I would NOT use the router to do that, but rather display the components dynamically. That's how Angular thought about it : depending on a condition, you display the component you wish.

Also, another solution you can think of, is to make a single component that sets up himself depending on the configuration your provide to it, instead of having dozen of components that do almost the exact same thing.

MGX
  • 2,534
  • 2
  • 14
  • Thank you, truly great answer. "depending on a condition, you display the component you wish" So would I use ngSwitchCase? Or is there other better constructs that allow this? – Quick Maths Sep 24 '22 at 14:02
  • Why do you think did firebase then show such an example with a router? Because he didn't have this error-validation dependency like me? Or maybe because when using ngSwitchCase then you cannot have the route change based on which switch case took place? Or can you? – Quick Maths Sep 24 '22 at 14:03
  • No, you would use [dynamic components](https://angular.io/guide/dynamic-component-loader). Less code, better readability ! And I do not know for your second question, because first I am not in your project, and second I will not watch a 15 minutes video, hope you understand ... But it can be for the sole purpose of demonstrating how the router works, who knows ! – MGX Sep 24 '22 at 14:07
  • Ok thanks, dynamic components, that's awesome. And yes I guess the first 10 seconds of the video wouldn't suffice to find out why he wanted that. Understandable. Thank you! – Quick Maths Sep 24 '22 at 14:12
1

You could do the requests in the parent component.

Then, you can pass information through routing. Normally, you would use route parameters for this. But for complex objects which you don't want to display in your URL you can do something like this:

this.router.navigate(["childComponentPath"], { state: { data: <response> }});

and then in your child component you can retrieve the data like this:

this.data = this.router.getCurrentNavigation().extras.state["data"]

UPDATE:

So while creating a StackBlitz demo for this solution, I found there is a tiny problem if you refresh the page. Because the data are passed on navigation, if you refresh the page you lose the data.

So you will either have to redirect back to the "home" route (already implemented in StackBlitz), or use a different solution altogether.

As an alternative, you could make the requests in your child components and communicate the error with a service to your parent component.

Vasileios Kagklis
  • 784
  • 1
  • 4
  • 14
  • Wow this is a very good way to pass complex data through the router. I think I will actually go with this approach, I like it the most. – Quick Maths Sep 24 '22 at 14:05
  • According to this post you cannot pass complex objects through the router. https://stackoverflow.com/questions/41651925/can-angular-2-pass-a-complex-object-along-the-router Is this post wrong? Does it work like with your post? – Quick Maths Sep 24 '22 at 14:07
  • @QuickMaths it works, but what I fail to understand is that the OP is giving you a way to route to the child component, whereas what you want is a way to let the parent know that your child component has some error. I'm not sure this is the right approach ! – MGX Sep 24 '22 at 14:08
  • "How can the parent-component display what went wrong in the API request?" That's what I wrote. I could do the request in the parent. But still sceptic because of: https://stackoverflow.com/questions/41651925/can-angular-2-pass-a-complex-object-along-the-router – Quick Maths Sep 24 '22 at 14:10
  • my title is misleading, I will correct it to API-request-errors from children – Quick Maths Sep 24 '22 at 14:14
  • @QuickMaths No because the link you provided uses a way that serializes the object in the URL, while the way I provided doesn't do that. – Vasileios Kagklis Sep 24 '22 at 14:14
  • Oh ok, so your solution would actually be a solution to this question as well? -> https://stackoverflow.com/questions/41651925/can-angular-2-pass-a-complex-object-along-the-router If you approve & allow, then I can post a link to your answer to my question as an answer to his question. Or you could do that. – Quick Maths Sep 24 '22 at 14:16