Angular2 - one route, multiple components

645 views
Skip to first unread message

Luke

unread,
Sep 14, 2016, 6:25:35 PM9/14/16
to AngularJS
Hey,

this is my issue - I want to have one route for products with path: ':slug' meaning urls like these: example.com/my-product , example.com/another-product, example.com/product-number-three, (slug after '/' changes) and another route for brands also with path like this: ':slug' for urls like example.com/adidas, example.com/nike, example.com/gooogle.

What I'm looking for is a route with the path I mentioned above. It would first try to use one component(BrandComponent), on successful query into db with slug from params -> using it, otherwise going for another component(ProductComponent) repeating the same, else redirecting to 404.

Is there a way to achieve this?

Lucas Lacroix

unread,
Sep 14, 2016, 6:42:37 PM9/14/16
to AngularJS

Why not have an intermediate component that decides what type of thing to display? That seems the easiest way to do this.

In other words, your route would go to the "figureOutType" component that contains a child component for each type of thing you might display. Then you can use ngSwitch to only show the appropriate child component.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
--
Lucas Lacroix
Computer Scientist
Advanced Technology Division, MEDITECH

Luke

unread,
Sep 15, 2016, 5:14:56 AM9/15/16
to AngularJS
Thanks, that's actually a great idea ! Just to make sure - in template of the parent component, I have an ngSwitch on let's say a <div [ngSwitch]="type">, in it there are two child components <my-product *ngSwitchCase="'P'">.. and <my-brand *ngSwitchCase="'B'">.. also these two components are two-way and have @input properties of slug?

Lucas Lacroix

unread,
Sep 15, 2016, 8:12:48 AM9/15/16
to AngularJS

Yeah. That's what I was thinking.


On Thu, Sep 15, 2016, 05:15 Luke <luk.g...@gmail.com> wrote:
Thanks, that's actually a great idea ! Just to make sure - in template of the parent component, I have an ngSwitch on let's say a <div [ngSwitch]="type">, in it there are two child components <my-product *ngSwitchCase="'P'">.. and <my-brand *ngSwitchCase="'B'">.. also these two components are two-way and have @input properties of slug?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Luke

unread,
Sep 15, 2016, 1:23:35 PM9/15/16
to AngularJS
Great, thanks again
Reply all
Reply to author
Forward
0 new messages