2016-11-22 68 views
0

我有很多的組件和進口部分顯得有些力不從心:匯入所有組件Angular2

import {comp1} from './components/comp1/comp1'; 
import {comp2} from './components/comp2/comp2'; 
.... 

是否有寫通用進口的方式?喜歡的東西

import * from './components/'; 
+2

我使用一箇中間文件」 ./components/components'那導出組件中的每個文件。然後,我使用: 從'./components/components'導入{comp1,comp2}; –

+0

是的,我知道這個訣竅。只是好奇,如果有一個更優雅的解決方案 –

回答

1

可以使用導入與*和使用as關鍵字來創建一個變量在組件中使用。例如:

import * as jwt from 'angular2-jwt/angular2-jwt'; 

然後在你的組件稱其爲以下幾點:

// ... 
    console.log(jwt.AuthConfig); 
+0

這不適用於文件夾 –

+0

這僅適用於導出多個選項的文件: 'export const a:export const b;' –

1

這是index.ts文件。

我通常會在該文件夾中編寫一個index.ts文件夾,並將所需的所有內容放入索引文件中。

例如:

path/component/index.ts

export * from './child component 1/index'; 
export * from './child component 2/index'; 
export * from './child component 3/index'; 
export * from './child component 4/index'; 
export * from './child component 5/index'; 
... 

some/component/*.component.ts

進口*從 '路徑/組件/索引'

+0

這不起作用,顯示爲預期錯誤 – Nabeel