2017-06-23 35 views
0

如果我有一個服務返回多個數據點,並且這些服務需要在多個html部分中顯示(在下面三種情況下)。什麼是組織&架構師的最佳方式?Angular 2最佳實踐 - 哪一個HTML作品被限定爲組件

請注意,此處的區域和屬性始終顯示在一起。

有三種選擇,我有點困惑之間的第一和第二,哪一個選擇。

  1. 創建三個組件:TemplateComponent,SectionsComponent & PropertyComponent?
  2. 創建兩個組件:一個用於模板的組件和另一個用於區段和屬性的組件?
  3. 創建一個組件:所有三個部分的一個組件。

我們在CLI中使用Angular〜2.0。

樣品服務響應

Template_A 
    - Section 1 
     - Property a 
     - Property b 
    - Section 2 
     - Property a 
     - Property b 
Template_B 
    - Section 1 
     - Property a 
     - Property b 
    - Section 2 
     - Property a 
     - Property b 

樣品UI enter image description here

回答

0

您可以創建一個基本組件,並建立儘可能多的子組件所需並將數據傳遞到子組件。

在你的情況下,你將創建三個組件。

  1. TemplateComponent
  2. SectionsComponent
  3. PropertyComponent

    • SectionsComponentTemplateComponent的一個子組件,其 將接收切片數據作爲輸入。
    • PropertyComponent將是孩子SectionsComponent將接收屬性數據作爲輸入。

參考https://angular.io/guide/component-interaction獲取更多信息。