简体   繁体   English

Javascript Es6默认导出

[英]Javascript Es6 default export

I just want to double check since I am trying to do this ES6 export default: 我只想仔细检查一下,因为我正在尝试执行此ES6导出默认设置:

var Member = function(){}
export {
    Member as default
 };

JShint is error-ing out with this syntax (es6 enabled) but I thought it was valid. JShint使用这种语法(启用了es6)出错了,但是我认为它是有效的。 Is this really invalid or what is a valid way for writing a default export with the export syntax as 这真的是无效的,还是使用export语法编写默认导出的有效方法是

 export {
    Member 
 };

I was using this as reference: http://www.2ality.com/2014/09/es6-modules-final.html The example they gave was: 我使用它作为参考: http ://www.2ality.com/2014/09/es6-modules-final.html他们给出的示例是:

//------ module1.js ------
export default 123;
//------ module2.js ------
const D = 123;
export { D as default };

Why is this module2's 2nd line valid? 为什么此module2的第二行有效? (or is it?) (或者是?)

(As so often) this is is jshint's fault. (通常)这是jshint的错。 The line is indeed valid, ES6 Export syntax does permit the use of any IdentifierName - which includes keywords such as default - for the exported name of an ExportClause . 该行确实有效, ES6导出语法确实允许将任何IdentifierName (包括诸如default关键字)用作ExportClause的导出名称。

I would however discourage from using it. 但是,我不鼓励使用它。 Default exports are much easier to write and read in the export default notation, such as 默认导出更容易以export default表示法来编写和读取,例如

var Member = function() {};
export default Member;
// or
export default function Member() {}

Admittedly export default Member; 允许export default Member; is not exactly equivalent to export { Member as default } but unless you are try to reassign it (you're not, right?) it doesn't make a difference. 并不完全等同于export { Member as default }但是除非您尝试重新分配它(您不是,对吧?),否则没有什么区别。

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

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