es6 equivalent for module.exports

岁酱吖の 提交于 2019-12-12 02:34:32

问题


What's the ES6 equivalent for module.exports

I want to get the value of foo from an import statement

module.exports = {
    foo: function (a) {
    }
}

Tried:

export default {
    foo: function (a) {
    }
}

The way first one is imported is using:

var file;
var filename = root + "/" + fileStats.name;
file = require(path.resolve(filename));

I want to use ES6 import statement. I read somewhere that this isn't supported however would like to still know if there's a work around this.


回答1:


Not sure what you're trying to do because in the code you supplied you did not consume the actual foo method from the object you imported.

But if I understand correctly, you could achieve this in one of 2 ways:

export default function foo(a) { };

and consume the module with:

import foo from './<filename>.js';

Or alternatively, don't use the default export:

export function foo(a) {};

and consume with:

import { foo } from './<filename>.js';


来源:https://stackoverflow.com/questions/37772749/es6-equivalent-for-module-exports

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