Summary ES6ES6 modules:
exportsExports:
You have 2two types of exports:
- Named exports
- Default exports, Max 1a maximum one per module
Syntax:
// Module A export const importantData_1 = 1; export const importantData_2 = 2; export default function foo () {} Imports:
The type of export (i.e., named or default exports) affects how to import something:
- For a named export we have to use curly braces and the exact name as the declaration (i.e. variable, function, or class) which was exported.
- For a default export we can choose the name.
Syntax:
// Module B, imports from module A which is located in the same directory import { importantData_1 , importantData_2 } from './A'; // forFor our named imports // syntaxSyntax single named import: // import { importantData_1 } // forFor our default export (foo), the name choice is arbitrary import ourFunction from './A'; Things of interest:
- Use a comma separated-separated list within curly braces with the matching name of the export for named export.
- Use a name of your choosing without curly braces for a default export.
Aliases:
Whenever you want to rename a named import this is possible via aliases. The syntax for this is the following:
import { importantData_1 as myData } from './A'; Now we have imported importantData_1 but, but the identifier is myData instead of importantData_1.