简体   繁体   English

具有6to5的ES6默认模块语法

[英]ES6 Default Module Syntax with 6to5

Is it possible to recreate the following with ES6 module syntax? 是否可以使用ES6模块语法重新创建以下内容?

var foo = {};
module.exports = foo;

ES6 has support for adding the declarative keyword to the expression, like so: ES6支持将声明性关键字添加到表达式中,如下所示:

export var foo = 'bar';

However, when run through 6to5 , this generates: 但是,通过6to5运行时,将生成:

var foo = exports.foo = 'bar';

Is it possible to use this syntax in conjunction with the default keyword, in order to generate the top code snippet? 是否可以将此语法与default关键字结合使用,以生成顶部代码段?

You must export foo entity with default keyword: 您必须使用default关键字导出foo实体:

var foo = {};
export default foo;

It will generate exactly what do you want. 它将确切生成您想要的东西。

PS : You can export only one default variable per module and can import it without curly brackets: PS :每个模块只能导出一个default变量,并且可以不使用大括号将其导入:

import foo from 'some_module';

If you're exporting multiple variables: 如果要导出多个变量:

export var foo = 10;
export var boo = 'something';

Then you must import them using curly brackets: 然后,您必须使用大括号将它们导入:

import { foo, boo } from 'some_module';

In that case 6to5 will generate a little more complicated result than your example. 在这种情况下, 6to5会比您的示例产生更复杂的结果。

More about ES6 modules read here 有关ES6模块的更多信息,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM