0
0

More than 3 years have passed since last update.

エクスポートの使い方

Posted at

プログラミングの勉強日記

2020年6月8日 Progate Lv.85
JavaScriptⅴ

デフォルトエクスポートとは

 export defaultのことである。ファイルがインポートされると同時に「export default 値」の値がインポートされる。なので、エクスポートしたときの値の名前と、インポートしたときの値の名前が違ったとしても問題ない。
 ※デフォルトエクスポートは1ファイルに1つの値しか使えない
  →複数の値をエクスポートしたいときは名前付きエクスポートを使う。

名前付きエクスポート

 defaultを書かずに名前を{}で囲む。インポートするときは、inport {値の名前} from "./ファイル名"と指定する。複数の値をインポートするときは,(コンマ)で区切る。

dogData.js
const dog1 = new Dog("レオ",4,"チワワ");
const dog2 = new Dog("ベン",2,"プードル");
export {dog1,dog2};
script.js
import {dog1,dog2} from "./dogData";
0
0
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
0
0