繁体   English   中英

如何正确地将一个普通的 JS 文件导出和导入到另一个 JS 文件中?

[英]How do I correctly export and import a plain JS file into another JS file?

我需要导出和导入一个普通的 js 文件才能正确地进行键盘导航。 我正在遵循此处的示例,并使用 ES5 的导入和导出模式,但它并未将一个 js 文件链接到另一个文件。 https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html#这是我的codepen.io链接https://codepen.io/ankita -sharma-the-flexboxer/项目/编辑器/DzdmBE

module.exports = PopupMenu;
var myModule = require('./popuplinks');

有多种方式导出和导入 JavaScript 模块,过去我们使用的是您提供的格式的 CommonJS 模块,它们可以通过以下方式使用。

// module.js
const myModule = "something"
module.exports = myModule
// And then if you want to import in another file, you're using following sentence.
const module = require("./module")

实际上我们正在使用类似 ES6 的导入,你可以在 MDN 上阅读它们,我附上了一个 ES6+ 导出的小样本。

// module.js
const myModule = "Something"
export { myModule }
// And then
import {myModule} from './module'

实际上,您应该阅读可以解决您的问题的帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM