javascript 未捕获的语法错误:请求的模块“./add.js”不提供名为“add”的导出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54590951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught SyntaxError: The requested module './add.js' does not provide an export named 'add'
提问by Liga
I am trying to learn ES6 imports and exports but I ran into an error that is not letting me import my module. I also tried import .. from 'add.js'without ./ but still no luck.
我正在尝试学习 ES6 导入和导出,但我遇到了一个错误,无法导入我的模块。我也试过import .. from 'add.js'没有 ./ 但仍然没有运气。
Uncaught SyntaxError: The requested module './add.js' does not provide an export named 'add'
未捕获的语法错误:请求的模块“./add.js”不提供名为“add”的导出
My folder structure looks like this
我的文件夹结构是这样的
C:\xampp\htdocs\es6\import.export\
- index.html
- app.js
- add.js
index.html
索引.html
<html>
<head>
<script type="module" src="app.js"></script>
</head>
<body>
</body>
</html>
app.js
应用程序.js
import { add } from './add.js'
console.log(add(2,3))
add.js
添加.js
export default function add (a, b) {
// export default function (a, b) { <-- does not work either, same error
return a + b;
}
采纳答案by Zak
There are two kinds of exports: named exports (several per module) and default exports (one per module). It is possible to use both at the same time, but usually best to keep them separate.
有两种导出:命名导出(每个模块几个)和默认导出(每个模块一个)。可以同时使用两者,但通常最好将它们分开。
If you want to import the module's default, the curly braces '{}' are not needed :
如果要导入模块的默认值,则不需要大括号“{}”:
You can use curly braces '{}' for named exports :
您可以将花括号“{}”用于命名导出:
回答by molamk
Option 1
选项1
Name your export instead of using default. It should look like this
命名您的导出而不是使用default。它应该是这样的
// add.js
export const add = (a, b) => a + b;
// OR
// export const add = function(a, b) { return a+b };
// app.js
import { add } from './add';
Option 2
选项 2
Use the export defaultsyntax. It looks like this
使用export default语法。看起来像这样
// add.js
export default function add(a, b) {
return a + b;
}
// app.js
import add from './add';

