简体   繁体   中英

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

I need to export and import a plain js file to work keyboard navigation correctly. I am following the example here,and using import and export pattern of ES5 but it is not linking one js file to another. https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html# This is my codepen.io link https://codepen.io/ankita-sharma-the-flexboxer/project/editor/DzdmBE

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

There are multiple ways of exporting and importing JavaScript modules, in the past, we were using CommonJS modules which are in a format you've presented, they can be used in following way.

// 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")

Actually we're using ES6-like imports, you can read about them at MDN, I'm attaching a small sample of ES6+ export.

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

Actually you should read post that will resolve your issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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