Import with or without curly brackets in ES6 [duplicate]

。_饼干妹妹 提交于 2021-02-10 06:26:36

问题


What is the difference between:

import Title from './title.js'

and

import { Title } from './title.js'

?

I think it has some connection with export default Title; and export const Title; but I don't know.


回答1:


as given in developer.mozilla.org

It is possible to have a default export (whether it is an object, a function, a class, etc.). The import statement may then be used to import such defaults.

The simplest version directly imports the default:

import myDefault from '/modules/my-module.js';

References : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import




回答2:


A module may declare multiple exports. For instance export const Title; and export const SubTitle;. When you import such a module you get an Object whose keys are the exports you declared.

You can then use function parameter object destructuring - a feature available in ES6 - to select only exports you need from the Object.

Note that parameter destructuring is not available if you use export default since import will not necessarily return an object, unless that is what you exported.



来源:https://stackoverflow.com/questions/46579940/import-with-or-without-curly-brackets-in-es6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!