导入模块时 export { foo as bar } 会影响什么

what will export { foo as bar } affect when importing module

我对这段代码感到困惑 here

//------ underscore.js ------
export default function (obj) {
    ...
};
export function each(obj, iterator, context) {
    ...
}
export { each as forEach };

//------ main.js ------
import _, { each } from 'underscore';
...

export { each as forEach } 部分让我感到困惑。 当我导入这个函数时,我应该使用 import { each } from 'underscore'import { forEach } from 'underscore'?

当我使用 main.js 中的函数时, export { each }export { eash as forEach } 之间有什么区别?

The export { each as forEach } part confused me.

表示"export the value of the local variable each under the name forEach".

When I import this function, should I use import { each } from 'underscore' or import { forEach } from 'underscore'?

import { forEach } from 'underscore'

因为这是模块导出的内容。

When I use the function in main.js , what will be the difference between export { each } and export { eash as forEach } ?

export {each} 导出名称 each 下的值。另一个见我的第一个回答。

使用函数没有任何区别,只是您使用不同的名称。


FWIW,同样的事情可以在导入端完成:

import { forEach as foo} from 'underscore'

现在您可以将该函数称为 foo 而不是 forEach