投稿数 585
公開から 2940

JavaScriptのファイルを分けるES6編


Categories: javascript

はじめに

ES6以外にもnodejsのmoduleとかでもできるみたいですけど、今回はES6でのやり方です。

ざっくりいうと

exportをつくって、それをimportします。

くわしく!

exportの書き方

// module.jsを用意します export function hello() { return “Hello”; }

import時の書式

// main.js import {hello} from ‘module’; // or ’./module.jsからexport function「hello」を読み込む’ let val = hello(); // val is “Hello”;

参考

https://hype.codes/how-include-js-file-another-js-file