2017-02-18 51 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