在 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 中更改什么才能做到这一点?
最佳答案
我建议将所有变量导入为模块命名空间对象,然后导出该对象:
import * as variables from './variables';
export { variables };
另请参阅:this article 中的“模块 namespace 对象”来自 Mozilla。
关于javascript - 将多个变量导出为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56732451/