繁体   English   中英

ES6的出口和花括号

[英]ES6's export and curly braces

我看到一个代码已经发布在聊天频道中。 在他的代码的最后是

export {UserInformation};

有些团体说法语错了。 有些人说只要变量存在就可以了。

哪个群体是对的? 这是我第一次看到这种语法。 我从未见过出口花括号。 我只在导入时使用它们。 像这样

import {method} from 'someModule';

如果我正在写它,我会把它写成

export default UserInformation;

我不想用错误的信息污染我的大脑。 让我知道哪个出口是正确的。

语法是正确的。 这个

export {UserInformation};

是简写

export {UserInformation as UserInformation};

这就像在做

export const UserInformation = {};

在定义UserInformation

能够从定义它的不同位置的模块中导出某些东西是有用的(例如,为了便于阅读)。

在这种情况下,您可以像这样导入UserInformation

import {UserInformation} from 'UserInformation.js';

请注意export default UserInformation; 等同于这一点。 在这种情况下,您将UserInformation作为默认模块导出。 要在这种情况下导入UserInformation ,您需要:

import UserInformation from 'UserInformation.js';

这是简写

import {default as UserInformation} from 'UserInformation.js';

这篇博文是关于该主题的精彩读物。

暂无
暂无

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

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