Export an imported module

不打扰是莪最后的温柔 提交于 2019-12-04 09:56:01

问题


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 actually has very nice tables listing all the possible import and export 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.



来源:https://stackoverflow.com/questions/34444909/export-an-imported-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!