ES6: Export
Difference between export default
and export
- Both
export
andexport default
can be used to exportvar
, ‘let’, ‘cont’,class
,function
, etc. - You can use
import + name
to import it into a file. - In a file or module, can has many
export
,import
, but only oneexport default
. - Import add
{}
when throughexport
,export default
is not needed.
Export
// a.js
export const a = 1
export function hi {
console.log('hi')
}
// b.js
import { a, hi } from 'a'
Export default
// a.js
const a = 1
export default a
// b.js
import a from 'a'
Syntax
// exports a function declared earlier
export { myFunction };
function myFunction(){};
// exports a constant
export const foo = 'bar'
// default exports function
export default function() {}
// default exports class
export default class {}
// default export can be imported with any name
let k; export default k = 12; // in file test.js
import m form './test'
Reference:
ES6:export default 和 export 区别 MDN web doc: export