导入所有Angular2组件

6

我有很多组件,导入部分看起来很臃肿:

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

有没有一种编写通用导入的方法?类似于:
import * from './components/';

2
我使用一个中间文件'./components/components',它导出了组件中的每个文件。然后我使用: import {comp1, comp2} from './components/components'; - Guilherme Meireles
是的,我知道这个技巧。只是好奇是否有更优雅的解决方案。 - Kertis van Kertis
有没有找到解决方法的运气? - hamada147
2个回答

8
你可以使用*来导入内容,并使用as关键字创建一个在组件中使用的变量。例如:
import * as jwt from 'angular2-jwt/angular2-jwt';

然后在您的组件中按以下方式引用它:

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

这仅适用于导出多个选项的文件: export const a: export const b; - David

3
这是关于 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

某些组件/ *.组件.ts

import * from 'path/component/index'

从路径/组件/索引导入*


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接