Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Tại sao file HTML không tìm thấy hàm JavaScript từ module nguồn?


Điều này có thể xảy ra nếu bạn chưa dùng câu lệnh "export". Hãy dùng "export" trước hàm sẽ được import vào file script. File JavaScript demo.js:

demo.js

console.log("function will import"); export function test(){ console.log("Imported!!!"); }

File "index.html" import hàm trên:

index.html

Ví dụ

     Document        

Chạy: lưu file anyName.html(index.html), chuột phải chọn "Open with Live Server" trong VS Code.

Đầu ra từ file demo.js có hàm test():

Tại sao file HTML không tìm thấy hàm JavaScript từ module nguồn?