迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

使用 JavaScript 从另一个文件导入变量

作者:迹忆客 最近更新:2022/12/16 浏览次数:

要在 JavaScript 中从另一个文件导入变量:

  1. 从文件 A 导出变量,例如 export const str = 'Hello world'
  2. 将文件 B 中的变量导入为 import { str } from './another-file.js'
  3. 下面是从名为 another-file.js 的文件中导出两个变量的示例。

another-file.js

// 👇️ named export
export const str = 'Hello world';

// 👇️ named export
export const str2 = 'one two three';

以下是我们如何将变量导入名为 index.js 的文件中。

index.js

// 👇️ named import
import {str, str2} from './another-file.js';

console.log(str); // 👉️ "Hello world"

console.log(str2); // 👉️ "one two three"

如果必须,请确保更正指向 another-file.js 模块的路径。 该示例假定 another-file.jsindex.js 位于同一目录中。

例如,如果 another-file.js 位于一个目录之上,则必须导入为 import {str} from '../another-file.js'

导入时,我们将变量名用大括号括起来。 这称为命名导入。

我们用来导出和导入变量的语法称为 JavaScript 模块。

为了能够从不同的文件导入变量,必须使用命名导出或默认导出来导出它。

上面的示例使用命名导出和命名导入。

命名导出和默认导出和导入之间的主要区别在于,每个文件可以有多个命名导出,但只能有一个默认导出。

让我们看一个示例,说明如何导入使用默认导出导出的变量。

这是 another-file.js 的内容。

another-file.js

const str = 'Hello world';

// 👇️ default export
export default str;

下面是我们如何使用默认导入来导入变量。

index.js

// 👇️ default import
import str from './another-file.js';

console.log(str); // 👉️ "Hello world"

请注意 ,我们没有将导入内容用花括号括起来。

我们也可以在导入变量时使用不同的名称,例如 fooBar.

// 👇️ default import
import fooBar from './another-file.js';

console.log(fooBar); // 👉️ "Hello world"

这有效,但令人困惑,应该避免。

如果要将变量(或箭头函数)导出为默认导出,则必须在第一行声明它并在下一行导出它。 您不能在同一行上声明和默认导出变量。

根据我的经验,大多数真实世界的代码库只使用命名导出和导入,因为它们可以更轻松地利用 IDE 进行自动完成和自动导入。 我们也不必考虑使用默认导出或命名导出导出哪些成员。

也可以混合搭配。 以下是同时使用默认导出和命名导出的文件示例。

another-file.js

const str = 'Hello world';

// 👇️ default export
export default str;

// 👇️ named export
export const num = 100;

这是导入这两个变量的方法。

index.js

// 👇️ default and named imports
import str, { num } from './another-file.js';

console.log(str); // 👉️ "Hello world"

console.log(num); // 👉️ 100

我们使用默认导入来导入 str 变量,使用命名导入来导入 num 变量。

请注意 ,每个文件只能有一个默认导出,但我们可以根据需要拥有任意多个命名导出。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便