javascript - 将多个变量导出为一个

标签 javascript typescript object ecmascript-6

在 TS 中,我有来自 class.tsClass1、来自 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/

相关文章:

javascript - 如何在使用 lodash 省略空值的同时合并两个对象

javascript - 没有旋转的转换属性的 CSS 转换

javascript - AWS Amplify Authenticator React Native Tab Bar Navigation白屏,而不是呈现我的应用

typescript - 使用nest.js运行docker命令

Typescript 类型断言 - 与可选成员的接口(interface)

JavaScript 对象文字长度 === 未定义?

javascript - 是否有义务运行 Node 服务器以执行基于 Angular 的应用程序

javascript - 如何停止编辑检查器代码中的选择框?

angular - 使用实现自己组件的子模块

javascript - Javascript 对象可以像数组一样访问吗?