ES6的导出和花括号 [英] ES6's export and curly braces

查看:75
本文介绍了ES6的导出和花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在聊天频道中看到一个代码。在他的代码结尾处是

I saw a code got posted in a chat channel. At the very end of his code is

export {UserInformation};

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

There were groups saying that the syntax is wrong. Some were saying it is fine as long as the variable exists.

那么哪个组是正确的?我也是第一次看到这种语法。我从未在出口中看到花括号。我只在导入中使用了它们。像这样

So which group is right? It's my first time seeing this kind of syntax too. I've never seen curly braces in export. I've only used them in import. Like this

import {method} from 'someModule';

如果我正在写,我会写为

If I was writing it, I would write it as

export default UserInformation;

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

I don't want to pollute my brain with wrong information. Let me know which export is correct.

推荐答案

语法正确。此

export {UserInformation};

export {UserInformation as UserInformation};

这就像在做

export const UserInformation = {};

能够从定义了模块的其他位置的模块中导出某些内容非常有用(例如,为了提高可读性)。

It's useful to be able to export something from a module in a different place where it's defined (for readability, for instance).

在这种情况下,您将这样导入 UserInformation

In this case, you'd import UserInformation like this

import {UserInformation} from 'UserInformation.js';

请注意,导出默认用户信息; 与此等效。在这种情况下,您将 UserInformation 设为默认模块导出。在这种情况下,要导入 UserInformation ,您需要执行以下操作:

Please note that export default UserInformation; is not equivalent to this. In that case, you're making UserInformation be the default module export. To import UserInformation in that case, you'd do:

import UserInformation from 'UserInformation.js';

这是

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

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

This blog post is an excellent read about the topic.

这篇关于ES6的导出和花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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