JavaScript 中的模塊如何打包運(yùn)行(?三)
選項(xiàng) 1: src 法。將 index.html 與 change.js 放在同一個(gè)目錄里面。change.js 中定義了函數(shù) changeTimeFormat()。在 index.html 要使用該函數(shù),只需要通過 < script src="change.js"></script> 將其包含進(jìn)來即可。打開 index.html, 即顯示期望的結(jié)果。


選項(xiàng) 2: module-export-import 法。 將 index.html 與 change.js 放在同一個(gè)目錄里面。 ?change.js 中定義了函數(shù) changeTimeFormat(),且被導(dǎo)出 export function changeTimeFormat(table) { ... } 。index.html 中的 < script> 標(biāo)簽需要指定類型為模塊, type="module"。 使用 import 語句導(dǎo)入 change.js 中內(nèi)容,即,import * as change from './change.js' 。 用這種方法,直接打開 index.html, 不能顯示期望的結(jié)果。 需要先用 static-server 運(yùn)行一個(gè)本地 server, 然后在網(wǎng)頁瀏覽器輸入 http://localhost:9080/index.html 。 (如果使用 http-server, 則輸入 http://localhost:8080/index.html)

