ES6에서 모듈 사용

ES6에서 모듈 사용

  • 키워드 import가져 오기 모듈
  • 키워드 export가져 오기 모듈

수출

새 파일 생성mj.js

export function plus(a, b) {
    
    
  return a + b
}

수입

가져 오기는 모듈 유형의 ​​스크립트 태그에 배치되어야합니다.

import * as m from './mj.js';

alert(m.plus(1, 2));

import ()를 사용하여 모듈을 가져
오면 import ()가있는 스크립트는 유형을 module로 설정할 필요가 없습니다. import () 함수는 Promise 객체를 반환합니다.

import('./mj.js').then(r => {
    
    
  alert(r.plus('hello', 'world'))
})

완전한 코드

module암호

export function plus(a, b) {
    
    
  return a + b
}

html암호

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>es6 模块</title>
</head>
<body>

<script type="module">
    import * as m from './mj.js';
    alert(m.plus(1, 2));
</script>
</body>
</html>

추천

출처blog.csdn.net/BDawn/article/details/114886782