This question already has an answer here:
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
The import statements are used to import the exported bindings from another module
The curly braces ({}) are used to import named bindings and the concept behind it is called destructuring assignment The concept of destructuring assignment is a process that makes it possible to unpack the values from arrays or objects into distinct variables in the imported module
The curly braces ({}) are used to import named bindings
I would like to explain different types of imports in ES6 with the help of an example
Suppose we have a a module named Aninmals(Animals.js) let suppose the module exports a default binding
Man
and several other named bindings such asCat
,Dog
etcImport a single export from a module
In order to export a single export from another module (let say Cat) we can write it like this
Similarly for Dog
Import multiple exports from module
You can also import multiple modules as follows
Import an export with a more convenient alias
Rename multiple exports during import
But in the case to import Man into another module since it is a default export you can write it like thie
You can also mix the both the above variants for example
Import an entire module's contents
If you want to import everything you can use
Here, accessing the exports means using the module name ("Animals" in this case) as a namespace. For example, if you want to use Cat in this case you can use it like below
You can read more information about import here
you can read about destructuring here
is the way you need to import single, named exports from a module, in this case it is importing named exports
elements
andrenderLoader
frombase.js
.The
{ elements, renderLoader }
syntax is in many cases just syntactic sugar (called destructuring) added in recent versions of the ECMAScript standard.In this case, though, it is necessary to get only the named exports you want.
Please note that you can also pick new names for your variables like this:
which would then make the
elements
export available asnewNameForElements
etc.You can export more than 1 content from a single module.
For example at your code:
At
//1
, you import Everything from'./views/searchView';
At
//2
, there might be more content from'./views/base'
, but you import onlyelements and renderLoader
For more information: import MDN
Take the following example:
File to be imported, say importedFile.js:
Now in your main JS file, if you would do the following:
Here the variable
something
would get the value of the variable/function that has been exported as default in the importedFile.js file, i.e. the variabledefaultExport
. Now, if you do something like the following:It would import specifically
otherExport1
andotherExport2
variable and function and not thedefaultExport
andotherExport3
.You can also do something like the following to import all the variables by their names from importedFile.js:
Imports the default exported element as
Search
.Imports everything into
searchView
that has been exported.Imports a hand-picked number of named exported elements.
{} is used when you want to import part of an object. The * as searchView one will import all properties and methods in the searchView file.
Suppose './views/base' has 3 properties: elements, renderLoader, additionalParam (Assuming that all three have been exported as named exports in the file)
When doing
you import only those 2 specific properties
But when you do
you import all three properties in the object named base