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

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

问题描述

假设我正在创建一个具有多个文件的ES6库,但具有包含所有顶级定义的根文件。如何实现这个例子:

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

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

client.js
#这不行。
import {Sub} from'lib / main'

顶级文件(因为子级别是实现细节 - 稍后可能会被封装到单个最小化的文件中(但在开发时间是多个文件)。

解决方案

您必须重新导出:

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

您可以直接重新导出:

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

您可以在导出时重命名:

 从'./foo/sub'导出{Sub as MySub}; 

或重新导出所有内容:

  export'from'./foo/sub'; 


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).

解决方案

You have to re-export:

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

You can re-export directly like this:

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

You can rename when exporting:

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

Or re-export everything:

export * from './foo/sub';

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

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