Export an imported module

2019-03-24 07:53发布

问题:

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();

回答1:

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.



回答2:

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