Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - two components with the same route

In angularJS ui-router allows us to use 2 components with the same route using states (like in this example Angular UI Router: Different states with same URL?).

Is it possible to achieve the same behaviour in angular2? If it is could you link/provide some examples or workarounds?

The use case here would be something similar to Facebook or Twitter where the URL stays the same but the content changes depending on if you are logged in or not.

So far the only way I can think off to achieve this is using *ngIf in the 'parent' template to select selector of one of the two 'children' components. Something like this:

<home-logged-in *ngIf="authenticated()"></home-logged-in>
<home-logged-out *ngIf="!authenticated()"></home-logged-out>

Are there any recommended ways to do this?

Thanks

like image 628
dem00n Avatar asked May 05 '16 08:05

dem00n


Video Answer


1 Answers

You can have routes with parameters like

/article/:id/detail

Where /article/123/detail and /article/abc/detail lead to the same component.

See for example this tutorial https://angular.io/docs/ts/latest/tutorial/toh-pt5.html (Search for "Configure a Route with a Parameter")

like image 148
Günter Zöchbauer Avatar answered Oct 06 '22 09:10

Günter Zöchbauer