Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

import - ES6 Modules: transitively exporting symbols (i.e., from imported files)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...