Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript에서 가져오기 및 내보내기 이름 바꾸기

<시간/>

다음은 JavaScript에서 가져오기 및 내보내기의 이름을 바꾸는 코드입니다 -

참고 − 이 예제를 실행하려면 localhost 서버를 실행해야 합니다.

예시

<!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>Renaming imports and exports 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();
})

샘플.js

function testImport() {
   return "Module testImport has been imported" + "<br>";
   }
   function tellTime() {
      return new Date();
   }
export { testImport as test, tellTime };

출력

JavaScript에서 가져오기 및 내보내기 이름 바꾸기

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

JavaScript에서 가져오기 및 내보내기 이름 바꾸기