简体   繁体   中英

Export JavaScript Object without Node.js

I'm trying to export an Object from a.js file to another one. Actually I found many bits of help but with Node.js, and I'm not using it.

Do you think it's possible to do it without Node.js?

I've tried something:

//file1.js
var MyObject = {
property: value
};

export{MyObject};

//file2.js
import MyObject from 'file2.js';

This code gives me some errors:

  • Uncaught SyntaxError: Unexpected token 'export'
  • Uncaught SyntaxError: Cannot use import statement outside a module

EDIT: I've found another way to do what I was looking for without using the export method, thanks to the helpers !

//file1.js
var MyObject = {
property: value
};

export default MyObject;

//file2.js
import MyObject from 'file1.js';

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