0
我需要兩個組件用於我的選項卡模塊。合併組件以避免導入多個類
<tabs>
和
<tab>
要使用這個標籤我不得不進口這樣的:
import { Tabs, Tab } from '../../components/tabs/tabs.component';
在這種情況下,他們只有兩個組成部分。但我正在刨更多的「兒童組件」。有沒有合併它們的方法?
我需要兩個組件用於我的選項卡模塊。合併組件以避免導入多個類
<tabs>
和
<tab>
要使用這個標籤我不得不進口這樣的:
import { Tabs, Tab } from '../../components/tabs/tabs.component';
在這種情況下,他們只有兩個組成部分。但我正在刨更多的「兒童組件」。有沒有合併它們的方法?
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說明了這一點。
詩:有在一個文件中(Tab
和Tabs
無論是在tabs.component.ts
)多個組件被認爲是每Angular2 Style Guide一種不好的做法。建議將每個組件聲明爲其自己的文件並創建一個index
文件,該文件一次輸出它們 - 請參閱Create Import Barrels。
你想如何整合?他們是分開的班級。如果你想導入整個模塊,使用'import *作爲'../../ components/tabs/tabs.component'中的TabComponentModule –