Import as를 사용하면 다른 이름으로 명명된 모듈을 가져올 수 있습니다.
다른 이름으로 내보내기를 사용하면 명명된 모듈을 다른 이름으로 내보낼 수 있습니다.
다음은 Javascript에서 구성으로 가져오기 및 내보내기에 대한 코드입니다 -
예시
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-size: 18px;
font-weight: 500;
color: rebeccapurple;
}
</style>
</head>
<body>
<h1>Import as and Export as in JavaScript</h1>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to execute the imported function</h3>
<script src="script.js" type="module"></script>
</body>
</html> 스크립트.js
import {test,tellTime as showTime} from "./sample.js";
let resultEle = document.querySelector('.result');
document.querySelector('.Btn').addEventListener('click',()=>{
resultEle.innerHTML+=test();
resultEle.innerHTML+=showTime();
}) sample.js
function testImport() {
return "Module testImport has been imported" + "";
}
function tellTime() {
return new Date();
}
export { testImport as test, tellTime }; 출력
위의 코드는 다음과 같은 출력을 생성합니다 -

'여기를 클릭' 버튼을 클릭하면 -
