首页 > 解决方案 > 将多个变量合二为一

问题描述

在 TS 中,我有来自 class.ts 的Class1,来自helper.ts的一些函数,来自variables.ts的一些变量

例如,variables.ts看起来像这样:

export const test1 = 'test1';
export const test2 = 0;
export const test3 = 'test3';
export const test4 = 'test4';

然后使用 Webpack,我给 api.ts 提供了构建模块的入口。

api.ts

export { Class1 } from './class1';
export { helper1, helper2 } from './helper';
export * from './variables';

像这样,在我的 web 应用程序中,在 JS 中,我从这个模块中导入我需要的东西。但是,不是像这样导入每个变量:

import { test1, test2, test3, test4 } from 'api';

我想知道是否可以导入这样的东西:

import { variables } from 'api';
test1 = variables.test1;

或类似的东西:

import { variables.test1, variables.test3} from 'api';

我应该在 TS 中改变什么来做到这一点?

标签: javascripttypescriptobjectecmascript-6

解决方案


只需使用一个名为的包装器对象variables

export const variables = {
  test1: 'test1',
  test2: 0,
  test3: 'test3',
  test4: 'test4'
};

推荐阅读