สิ่งนี้อาจเกิดขึ้นหากคุณยังไม่ใช้คำสั่ง "export" ให้ใช้ "export" ก่อนฟังก์ชันที่จะถูกนำเข้าในไฟล์สคริปต์ ไฟล์ JavaScript มีดังนี้ ชื่อไฟล์ demo.js
demo.js
console.log("function will import"); export function test(){ console.log("Imported!!!"); }
นี่คือไฟล์ "index.html" ที่นำเข้าฟังก์ชันข้างต้น
index.html
ตัวอย่าง
Document
เพื่อรันโปรแกรมด้านบน ให้บันทึกชื่อไฟล์ "anyName.html(index.html)" และคลิกขวาที่ไฟล์ เลือก "Open with Live Server" ใน VS Code editor
ผลลัพธ์จากไฟล์ demo.js ที่มีฟังก์ชันชื่อ test()
