es6等效于module.exports [英] es6 equivalent for module.exports

查看:220
本文介绍了es6等效于module.exports的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

module.exports的ES6等效项是什么

What's the ES6 equivalent for module.exports

我想从导入语句中获取foo的值

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

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

尝试:

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

我想使用ES6 import语句.我在某处读到不支持此功能的信息,但仍然想知道是否有解决方法.

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.

推荐答案

不确定您要执行的操作,因为在您提供的代码中,您没有使用导入对象中的实际foo方法.

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) {};

并使用:

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

这篇关于es6等效于module.exports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆