首页 > 解决方案 > Angular 2+ 有一个主导入文件

问题描述

我有这样的数据不同的文件:

export const config1 = { stuff here }
export const config2 = { other stuff here }
export const config3 = { other stuff here }

然后我导入它们:

import { config1 } from 'some path';
import { config2 } from 'some path';
import { config2 } from 'some path';

我要做的是拥有一个包含所有导入的索引文件,所以我只导入 1?

所以代替上面我只需要做

import { whatever } from 'path'

然后我像这样使用它:

whatever.config1....

我怎样才能做到这一点?

标签: angulartypescriptangular6

解决方案


创建一个文件,例如configs.ts并从中导出:

export * from 'some path for config1';
export * from 'some path for config2';
export * from 'some path for config3';

然后你可以从这个文件中导入:

import { config1, config2, config3, whatever_else } from './path/to/configs';

推荐阅读