[英]"Cannot use import statement outside a module" when importing a self made class
I looked at several other questions and tried a lot around but i cant find the problem.我查看了其他几个问题并尝试了很多,但我找不到问题。
Those are my imports from the HTML file:这些是我从 HTML 文件中导入的:
<script src="./script/Graph.js" type="module"></script>
<script src="./script/Main.js"></script>
Graph.js:图.js:
export default class Graph {
constructor() {
this.vertices = [];
this.edges = [];
this.numberOfEdges = [];
}
...followed by several functions
}
Main.js:主.js:
import Graph from "./Graph.js";
Iam thankful for any tip:)我很感谢任何提示:)
To use import - export
in your files you need to specify the type of script file as module
要在文件中使用
import - export
,您需要将脚本文件的类型指定为module
so the corrected code would be所以更正的代码将是
<script src="./script/Graph.js" type="module"></script>
<script src="./script/Main.js" type="module"></script>
Read more on Modules阅读有关模块的更多信息
Extra:额外的:
You might come across CORS (cross origin resource sharing policy) while using import-export
, to overcome this you could use one of the following您在使用
import-export
时可能会遇到 CORS (跨源资源共享策略),要克服这个问题,您可以使用以下方法之一
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.