개발/javascript & typescript / / 2022. 8. 11. 01:53

JS module default 명령어

반응형

js export에 default라는 명령어가 있었다. js에 익숙하지 않아서 default 명령어 대해 처음 알았다.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module" src="main.js"></script>
</body>
</html>

js파일을 모듈로 사용하려면 script 태그에 type 옵션을 "module"로 추가한다.

 

// main.js
import hello from './hello.js';

hello();

hello로 받는 모듈이 있는데 hello.js에서 어떤 함수를 받을지를 다음과 같이 default로 명시한다.

 

// hello.js
export default function hello1(){
    console.log('Hello 1');
}

export function hello2(){
    console.log('Hello 2');
}

hello1 함수에 default가 선언되었으므로 hello.js를 {}로 분할하지 않고 import하면 기본적으로 hello1가 import된다.

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유