Angular2 - two components with the same route

蓝咒 提交于 2019-11-29 19:15:36

问题


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


回答1:


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")



来源:https://stackoverflow.com/questions/37045548/angular2-two-components-with-the-same-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!