Export an imported module

2019-03-24 07:04发布

I have two javascript modules that looks like this:

// inner/mod.js
export function myFunc() {
   // ...
}

// mod.js
import * as inner from "./inner/mod";

I would like to export myFunc from mod.js. How can I do this?

EDIT: I should clarify that the function is being exported as expected from inner/mod.js but I also want to export the funtion from the outer mod.js.

To those asking for clarification, I would like to achieve this:

// SomeOtherFile.js
import * as mod from "mod"; // NOT inner/mod

mod.myFunc();

2条回答
Emotional °昔
2楼-- · 2019-03-24 07:45

I believe what you are looking for is

export * from './inner/mod';

That will reexports all exports of ./inner/mod. The spec has actually a very nice table, listing all the possible export and import variants.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-24 08:01
// inner/mod.js
export function myFunc() {
   // ...
}

// mod.js
import { myFunc } from "./inner/mod";
export { myFunc };

Try to be explicit in what you import, the less the better, because of that I've changed your import in mod.js. If you do import *, you define a variable which will be the object of all names exports from that module you imported.

re-exporting is the same as making something of your own and exporting.

查看更多
登录 后发表回答