迹忆客 专注技术分享

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

如何在 JavaScript 中导出常量

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

使用命名导出来导出 JavaScript 中的常量,例如 export const A = 'a'export const B = 'b'。 导出的常量可以通过使用命名导入作为 import {A, B} from './another-file.js' 来导入。 我们可以在一个文件中拥有尽可能多的命名导出。

这是从名为 another-file.js 的文件中导出常量的示例。

// 👇️ named export
export const DB_PORT = 1234;

// 👇️ named export
export const URL = 'jiyik.com';

在定义变量的同一行上使用 export 与在声明常量后将其导出为对象相同。

const DB_PORT = 1234;

const URL = 'jiyik.com';

// 👇️ named exports
export {DB_PORT, URL};

下面是我们如何将常量导入一个名为 index.js 的文件中。

// 👇️ named imports
import {DB_PORT, URL} from './another-file.js';

console.log(DB_PORT); // 👉️ 1234

console.log(URL); // 👉️ "jiyik.com"

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

例如,如果要从一个目录向上导入,则可以 import {DB_PORT, URL} from '../another-file.js'

我们在导入常量时将它们的名称包裹在花括号中——这称为命名导入。

导入/导出语法在 JavaScript 中称为 ES6 模块。

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

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

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

如果我们尝试在单个文件中使用多个默认导出,则会收到错误消息。

const DB_PORT = 1234;

const URL = 'jiyik.com';

export default DB_PORT

// ⛔️ Error: Can't have 2 default exports in same file
export default URL

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

话虽如此,我们可以在单个文件中使用 1 个默认导出和尽可能多的命名导出。

让我们看一个使用默认导出和命名导出来导出常量的示例。

const DB_PORT = 1234;

// 👇️ default export
export default DB_PORT;

// 👇️ named export
export const URL = 'jiyik.com';

以下是导入这两个常量的方法。

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

console.log(DB_PORT); // 👉️ 1234

console.log(URL); // 👉️ "jiyik.com"

请注意,我们没有将默认导入包含在花括号中。

我们使用默认导入来导入 DB_PORT 常量,并使用命名导入来导入 URL 常量。

每个文件只能有一个默认导出,但可以根据需要拥有任意数量的命名导出。

根据我的经验,大多数现实世界的代码库都专门使用命名导出和导入,因为它们可以更轻松地利用您的 IDE 进行自动完成和自动导入。

我们也不必考虑使用默认导出或命名导出来导出哪些成员。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便