2017-02-18 54 views
1

我想知道,这是在做NG-内容VS路由器出口在角2

我的更好的方法。在父组件中使用ng-content,并在稍后创建子组件时,使子组件包含在父组件的选择器中。

例如:我想创建一个父视图作为组件。 a。 LandingComponet(Parent):

<div> 
Website Landing view 
.... 
... 
<ng-content></ng-content> 
.... 
... 
<footer></footer> 
... 
</div> 

b。 LoginComponent或RegisterComponent(子组件)

<app-landing> 
<div> 
Login view or Register view 
... 
</div> 
</app-landing> 

c。添加单独的路线的所有儿童组件

{ path: '', component: LandingComponent }, 
{ path: 'login', component: LoginComponent }, 
{ path: 'register', component: RegisterComponent } 

II。在父组件中使用router-outlet,并在路由文件中创建多个子路由。 例如:我想创建一个父视图作为组件。 a。 LandingComponet(Parent):

<div> 
Website Landing view 
.... 
... 
<router-outlet></router-outlet> 
.... 
... 
<footer></footer> 
... 
</div> 

b。 LoginComponent或RegisterComponent(子组件)

<div> 
Login view or Register view 
... 
</div> 

c。添加路由器在app.routing.ts

{ 
path: '', 
component: LandingComponent, 
children: [ 
{ path: 'login', component: LoginComponent }, 
{ path: 'register', component: RegisterComponent } 
] 
} 

既有优点和缺点,这坚持是我的问题,

我个人很喜欢第二(II)的方法,任何抬头会帮我到 做出决定。

回答

2

实际上,这两个彼此完全以不同的,或要么我们也不能说,你可以使用其中router-outlet需要ng-content。 基本上

NG-内容

用于当我们要包装在另一组件的某些内容,几乎不ng-content使用高达2或3级深的组件。 你也可以风格ng-content

但路由器outelt

当我们要配置的应用路由strucure使用。

当我们必须在一个插座中加载许多/没有组件。

还我会建议不要使用ng-content在你的使用情况,而不是使用router-outelt

+2

这个答案需要澄清。我不知道“什么时候配置路由结构”真的意味着或者与此有关。请澄清。 – 2017-03-24 02:21:17