[英]JavaScript, can't seem to export modules?
So I just started learning JavaScript. 所以我才开始学习JavaScript。 I am using plain JavaScript, and following the course on Code academy.
我正在使用普通的JavaScript,并遵循有关代码学院的课程。 Coming from C# I understood modules something like namespaces in C#.
来自C#,我了解模块,例如C#中的名称空间。 I am having trouble exporting, as well as importing them:
我在导出和导入它们时遇到了麻烦:
let Module = {
name: 'Hello'
};
export default Module;
This throws me an error - 'Uncaught SyntaxError: Unexpected token export' 这引发了一个错误-'未捕获的SyntaxError:意外的令牌导出'
I also tried the ES5 synthax: 我还尝试了ES5合成器:
let Module = {
name: 'Hello'
};
module.exports = Module;
This throws me another error - 'Uncaught ReferenceError: module is not defined'. 这引发了另一个错误-'未捕获的ReferenceError:模块未定义'。 I am using the newest version of Chrome, and I do not why JavaScript does not recognise these commands.
我使用的是最新版本的Chrome,所以我不理解JavaScript无法识别这些命令的原因。 It is very frustrating and cannot wrap my head around it.
这非常令人沮丧,无法将我的头缠住它。 Therefore, I would like to ask someone to please help me with this problem.
因此,我想请某人帮我解决这个问题。
If you're using the newest version of Chrome, it does support ES2015+ modules. 如果您使用的是最新版本的Chrome,则它确实支持ES2015 +模块。 You need to ensure your
script
tag says it's a module: 您需要确保您的
script
标签说它是一个模块:
<script type="module">
import name from './your-module-file.js';
console.log(name); // {name: "Hello"}
</script>
Then your first example will work. 然后,您的第一个示例将起作用。
But note that Module
is not a module. 但请注意,
Module
不是模块。 It's an object you're exporting from a module. 这是您要从模块中导出的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.