LoginSignup
1
2

More than 3 years have passed since last update.

【JavaScript】外部JSファイルをmoduleとして読み込む方法【ES6】

Posted at

scriptタグにtype="module"を忘れずに

index.html
<script type="module" src="js/main.js"></script>

webpackではないので拡張子の「.js」は必要

main.js
import { incidentDatalist } from './module.js'
console.log(incidentDatalist);

今回は変数としてexportする

module.js
export let incidentDatalist = [
  { year: 1192, name: '鎌倉幕府が開かれる'},
  { year: 1600, name: '関ヶ原の戦い'},
  { year: 794, name: '平安京へ遷都'},
  { year: 1923, name: '関東大震災が起きる'},
]

console.log(incidentDataList);などで外部JSの読み込みができたことを確認できる。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2