If you have started learning javascript in recent years, or using some font-end frameworks / libraries like React
then the use of import
and export
is very frequent. For those of you who are not aware of the ES6 new module syntax, you can distinguish and understand the difference between export by named and export default, but you can miss how to import using alias
and how to use them. for efficiency in the project.
Basic summary of import
and export
The Javascript module (as an example js my-module.js file) can export functions, objects, constants for external use. These are the basic conventions when working with javascript modules (code reuse, separation of concerns). In modules, we can export in 2 ways:
- Use separate
export
for functions and objects. - Use
export default
for a function or object.
When a module needs to use functions and objetcs from another module, some of the following methods can be imported:
- Import all exports into the current module:
import * from "my-module.js"
- Import some specific functions and objects:
import { func1, obj2 } from "my-module.js"
- Import function has default export:
import func from "my-module.js"
- Run the global code of the module but don’t import anything:
import "my-module.js"
- Import dynamic a module:
import("my-module.js").then((module) => { //...do something })
Most developers, especially React developers, we often use to import a function by name and import by default. There are a few cases where you can use import all or dynamic import (I will have an explanation of these import ways).
Importing alias can effectively solve in some cases, instead of using the named defined in exporting module, you can use a different name defined in the module using that export.
Why is it important to use import alias?
In many cases we import functions from different modules but they export the same named. At that time javascript reported the following error:
1 2 3 4 5 6 7 | Failed to compile <span class="token punctuation">.</span> <span class="token operator">/</span> Users <span class="token operator">/</span> my_name <span class="token operator">/</span> Project <span class="token operator">/</span> One <span class="token operator">/</span> src <span class="token operator">/</span> pages <span class="token operator">/</span> index <span class="token punctuation">.</span> js SyntaxError <span class="token operator">:</span> <span class="token operator">/</span> Users <span class="token operator">/</span> my_name <span class="token operator">/</span> Project <span class="token operator">/</span> One <span class="token operator">/</span> src <span class="token operator">/</span> pages <span class="token operator">/</span> index <span class="token punctuation">.</span> js <span class="token operator">:</span> Identifier <span class="token string">'Card'</span> has already been <span class="token function">declared</span> <span class="token punctuation">(</span> <span class="token number">6</span> <span class="token operator">:</span> <span class="token number">9</span> <span class="token punctuation">)</span> <span class="token number">4</span> <span class="token operator">|</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> Button <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'../components/button'</span> <span class="token number">5</span> <span class="token operator">|</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> Card <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'../components/card'</span> <span class="token operator">></span> <span class="token number">6</span> <span class="token operator">|</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> Card <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react-elements'</span> |
One of the most common cases is that we import components from an external library.
For example. Using component library directly in the page-level of the normal project will not be recommended, as this will be difficult to update and maintain the component later. Instead we often create custom components to suit the project requirements as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token comment">// Card.js in project/src/components/card</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> Card <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"third-library"</span> <span class="token keyword">const</span> <span class="token function-variable function">Card</span> <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span> <span class="token comment">// using third-library card </span> <span class="token comment">// custom something for your project needs</span> <span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token function-variable function">ListCard</span> <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span> <span class="token comment">// like above</span> <span class="token punctuation">)</span> <span class="token keyword">export</span> <span class="token punctuation">{</span> Card <span class="token punctuation">,</span> ListCard <span class="token punctuation">}</span> <span class="token comment">// ---</span> <span class="token comment">// Index.js in project/src/pages</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> Card <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'.../components/card'</span> <span class="token comment">// ... index page code</span> |
In the above example, we can see that the error cannot compile Card.js because the error declaration (there are 2 variables with the same name Card)
Use import alias to solve the problem
We will now use the alias import method to solve this problem, allowing us to import exporting module components with the same name.
When importing a named component from a module:
1 2 3 4 | <span class="token comment">// my-module.js</span> <span class="token comment">// ... code ..</span> <span class="token keyword">export</span> <span class="token punctuation">{</span> Something1 <span class="token punctuation">,</span> Something2 <span class="token punctuation">}</span> |
We can use alias with keyword as
:
1 2 | <span class="token keyword">import</span> <span class="token punctuation">{</span> Something1 <span class="token keyword">as</span> MySomething <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"my-module.js"</span> |
When importing a default export:
1 2 3 4 | <span class="token comment">// my-module.js</span> <span class="token comment">// ... code ..</span> <span class="token keyword">export</span> <span class="token keyword">default</span> Something |
We can use alias directly:
1 2 | <span class="token keyword">import</span> MySomething <span class="token keyword">from</span> <span class="token string">"my-module.js"</span> |
With the above examples, we can use import alias to solve the error compile import Card of the original problem:
1 2 3 4 5 6 7 8 9 10 11 | <span class="token comment">// Card.js in project/src/components/card</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> Card <span class="token keyword">as</span> LibCard <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"third-library"</span> <span class="token keyword">const</span> <span class="token function-variable function">Card</span> <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span> <span class="token comment">// using third-library card </span> <span class="token comment">// custom something for your project needs</span> <span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token function-variable function">ListCard</span> <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span> <span class="token comment">// like above</span> <span class="token punctuation">)</span> <span class="token keyword">export</span> <span class="token punctuation">{</span> Card <span class="token punctuation">,</span> ListCard <span class="token punctuation">}</span> |
If the Button is exported by default, we can also use alias:
1 2 3 4 5 6 7 8 9 10 11 | <span class="token comment">// Button.js in project/src/components/button</span> <span class="token keyword">import</span> LibButton <span class="token keyword">from</span> <span class="token string">"library-button"</span> <span class="token keyword">const</span> <span class="token function-variable function">MyButton</span> <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span> <span class="token comment">// code</span> <span class="token punctuation">)</span> <span class="token keyword">export</span> <span class="token keyword">default</span> MyButton <span class="token comment">// ---</span> <span class="token comment">// Index.js in project/src/pages</span> <span class="token keyword">import</span> Button <span class="token keyword">from</span> <span class="token string">'.../components/button/Button.js'</span> <span class="token comment">// ... index page code</span> |
Conclude
Hope you effectively use import alias in your projects, in the next articles I will explain about import all and dynamic import, thank you everyone for watching!