ฉันมีไฟล์ "สาม" ในโฟลเดอร์เดียว 1. index.html 2. index.js 3. helper.js
ฉันต้องการส่งออกโค้ดจาก helper.js
และนำเข้าโค้ดไปที่ index.js
(ฉันได้เชื่อมโยง index.js
ถึง index.html
แล้ว)
ฉันชอบ index.html แบบนั้น
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="btn">click</button>
<script src="./index.js"></script>
</body>
</html>
index.js
import btn from './helper'
btn.addEventListener('click', function () {
window.alert('i am clicked')
})
และสุดท้ายคือ helper.js
export let btn = document.getElementById('btn')
จากนั้นฉันก็เรียกใช้ไฟล์ index.html ในเบราว์เซอร์ Chrome
เบราว์เซอร์ Chrome ที่แสดงข้อความนี้ในคอนโซล
Uncaught SyntaxError: ไม่สามารถใช้คำสั่งนำเข้าภายนอกโมดูลได้
โปรดบอกฉันจะแก้ไขปัญหานั้นได้อย่างไร ฉันค้นหาใน Google และ YouTube ฉันทำสิ่งเดียวกันกับที่พวกเขาแสดง เป็นผลให้เบราว์เซอร์แสดงการนวดนั้น