微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在没有 ReferenceError 的情况下导入 javascript 模块?

如何解决如何在没有 ReferenceError 的情况下导入 javascript 模块?

我制作了一个导出 Javascript 文件,如下所示:

export const A = 1;
export const B = 2;
export const C = 3;

而且,我尝试将其导入另一个 Javascript 文件,如下所示:

import 'path/export_file.js'
console.log(A); // ReferenceError: A is not defined

我知道如果我执行以下操作,我可以修复它:

import A from 'path/export_file.js'
// or
import { A,B,C } from 'path/export_file.js'

但我想用 import 'path/export_file.js' 某些模块只执行 import'path/file',我可以使用从该模块导出的所有内容

我该怎么办?

还是我误会了什么?

解决方法

有两件事要知道:

  1. 您应该了解 ES6 中的导入/导出默认值和命名

  2. 正如@CertainPerformance 所提到的,除非模块分配给全局属性,否则您必须使用 {}。

    import { A,B,C } from 'path/export_file.js

  3. 如果同时使用 DefaultNamed,您可以像这样使用 *

    import * as Mix from 'path/export_file.js

感谢 @prosti's answer 的精彩回答。

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。