2016-07-24 46 views
0

我需要兩個組件用於我的選項卡模塊。合併組件以避免導入多個類

<tabs> 

<tab> 

要使用這個標籤我不得不進口這樣的:

import { Tabs, Tab } from '../../components/tabs/tabs.component'; 

在這種情況下,他們只有兩個組成部分。但我正在刨更多的「兒童組件」。有沒有合併它們的方法?

+1

你想如何整合?他們是分開的班級。如果你想導入整個模塊,使用'import *作爲'../../ components/tabs/tabs.component'中的TabComponentModule –

回答

1

Typescript允許你import the entire module into a single variable,並用它來訪問模塊導出。

在你的情況,如果你有一個'../../components/tabs/tabs.component';文件與一些線,如:

export class Tab { 
... 
export class Tabs { 

你可以在另一個文件,將它們導入到一個變量通過:

import * as TabsModule from '../../components/tabs/tabs.component'; 

的再使用TabsModule作爲前綴訪問它們。換句話說:

  • Tab變得TabsModule.Tab;和
  • Tabs變成TabsModule.Tabs

Typescript handbook on Modules說明了這一點。


詩:有在一個文件中(TabTabs無論是在tabs.component.ts)多個組件被認爲是每Angular2 Style Guide一種不好的做法。建議將每個組件聲明爲其自己的文件並創建一個index文件,該文件一次輸出它們 - 請參閱Create Import Barrels