ES6 模块:传递性导出符号(即从导入的文件中) [英] ES6 Modules: transitively exporting symbols (i.e., from imported files)

查看:17
本文介绍了ES6 模块:传递性导出符号(即从导入的文件中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在创建一个包含多个文件的 ES6 库,但有一个包含所有顶级定义的根文件.我怎么能实现这样的例子:

Suppose I'm creating an ES6 library with multiple files, but have a root file that contains all of the top-level definitions. How could I achieve something like this example:

lib/foo/sub.js
export class Sub{}

lib/main.js
import { Sub } from './foo/sub'

client.js
# This doesn't work.
import { Sub } from 'lib/main'

即,客户端仅从顶级文件导入(因为子级别是实现细节——稍后可能会被封装到一个缩小的文件中(但在开发时是在多个文件中).

I.e., where client only imports from the top-level file (since the sub levels are implementation details -- and later on will likely be encapsulated into a single minified file (but at development time are in multiple files).

推荐答案

你必须重新导出:

lib/main.js
import { Sub } from './foo/sub';
export { Sub };

您可以像这样直接重新导出:

You can re-export directly like this:

export { Sub } from './foo/sub';

导出时可以重命名:

export { Sub as MySub } from './foo/sub';

或重新导出所有内容:

export * from './foo/sub';

这篇关于ES6 模块:传递性导出符号(即从导入的文件中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆