2016-11-22 37 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