当前位置:网站首页>ES6 module import / export summary

ES6 module import / export summary

2022-06-13 08:42:00 huangxunlove

ES6 There are three types of module import and export :

  • Default import and default export
  • On demand import and export
  • Directly import and execute the code in the module

1、 Default import and default export

Default export Syntax :export default Default exported members

let n1 = 10
let n2 = 20
function show()
export default {
    
	n1,
	show
}

Default import Syntax :import Receiving name from ‘ Module identifier ’

import m1 from './01_m1.js'

matters needing attention : Only one use is allowed export default

2、 On demand import and export

On demand export Syntax :export Members exported on demand

export let s1 = 'aaa'
export function say(){
    }
export default {
    
	a:20
}

On demand import Syntax : import {s1} from ‘ Module identifier ’

import {
     s1, say } from './03_m1.js'

matters needing attention

  • Multiple on-demand exports can be used in each module
  • The name of the member imported on demand must be consistent with the name exported on demand
  • On demand import , have access to as Keyword to rename
  • On demand import can be used with default import
//info The output is an object , Is an object within the default export 
import info, {
     s1 as str1, say } from './03_m1.js'

Directly import and execute the code in the module

// The current file module is 05_m3.js
// Execute a... In the current module  for  Cyclic operation 
for (let i=0;i<3;i++){
    
	console.log(i)
}

-----------------------------------------

// Import and execute module code directly , You don't need to get external shared members of the module 
import './05_m3.js'
原网站

版权声明
本文为[huangxunlove]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270538542479.html