javascript - 以特定方式在JS中导入

标签 javascript typescript

在 TS 中,我有 class.tsClass1helper.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';
import * as variables from './utils/common-variables';
export { variables };

当我需要 variables.ts 的所有变量时,一切都很好。 我可以在我的 file.js 中执行此操作:

import { variables } from 'api';

然而,有时,我只需要一个变量。所以,我想知道是否可以导入这样的东西:

import { variables.test1 as test } from 'api';

最佳答案

不,那个特定的语法是 not possible – an ImportSpecifier can only really be a name ,而不是像您那样的属性访问表达式。

不过,您可以直接从 api/utils/common-variables 导入它们,而无需重新导出。

关于javascript - 以特定方式在JS中导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56774616/

相关文章:

javascript - 加载页面和定位元素后 JQuery 中的绝对定位

javascript - Parse.com 从其他类 Cloud Code 中的子查询获取用户

angular - 需要帮助正确输入函数参数

javascript - 在 Typescript 中使用 ES6 扩展运算符组合两个数组

Angular 2 TypeScript 错误 : Can't resolve all parameters for ProductsService

javascript - 属性 'submitAction' 在类型 '{}' 中缺失,但在类型中需要

javascript - 对不同选项卡中的 Web 应用程序更改使用react

javascript - js中如何删除div中的元素?

javascript - Angular 6 : How to prevent 2 subscribed variables in one Observable from cascading each other

php - 通过在超链接中使用 onClick 调用 javascript 函数