本文介绍了我们什么时候在javascript导入中使用'{}'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Javascript导入,当我们从另一个JS文件导入项目(函数,对象,变量)时使用花括号时我还不明白。

I am learning Javascript imports and I am yet to understand when we use curly braces while importing items(functions, objects, variables) from another JS file.

import Search from './models/Search';
import * as searchView from './views/searchView';
import { elements, renderLoader } from './views/base'
//elements is an object, renderLoader is a function


推荐答案

import { elements, renderLoader } from './views/base'

是从模块导入单个命名导出所需的方式,在这种情况下,它导入命名导出 元素 renderLoader 来自 base.js

is the way you need to import single, named exports from a module, in this case it is importing named exports elements and renderLoader from base.js.

{elements,renderLoader} 语法在很多情况下只是语法糖(称为 destructuring )在最新版本的ECMAScript标准中添加。

The { elements, renderLoader } syntax is in many cases just syntactic sugar (called destructuring) added in recent versions of the ECMAScript standard.

但在这种情况下,只需获得所需的指定出口。

请注意,您还可以为您的变量选择新名称,如下所示:

Please note that you can also pick new names for your variables like this:

import { elements as newNameForElements, renderLoader as newNameForRenderLoader } from './views/base'

然后会产生元素导出可用 newNameForElements 等。

which would then make the elements export available as newNameForElements etc.

这篇关于我们什么时候在javascript导入中使用'{}'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:02