简体   繁体   English

导入自制 class 时“不能在模块外使用导入语句”

[英]"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.

相关问题 导入类时“不能在模块外使用导入语句”和“未捕获的语法错误:意外的标识符” - "Cannot use import statement outside a module" and "Uncaught SyntaxError: Unexpected identifier" when importing a class “SyntaxError: Cannot use import statement outside a module” 导入模块时导入 Jest 中的副作用模块 - "SyntaxError: Cannot use import statement outside a module" when importing a module which imports a module for side effects in Jest 导入 React = &#39;SyntaxError: 不能在模块外使用导入语句&#39; - Importing React = 'SyntaxError: Cannot use import statement outside a module' 导入自定义 NPM 库时无法在模块外使用 import 语句 - Cannot use import statement outside a module when importing a custom NPM library 在 Next.js 中导入 react-hook-mousetrap 时出现“无法在模块外使用导入语句”错误 - "Cannot use import statement outside a module" error when importing react-hook-mousetrap in Next.js 将已编译的 Typescript 文件相互导入时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when importing compiled Typescript files to each other “未捕获的语法错误:无法在模块外使用导入语句”导入时 ReactJS - "Uncaught SyntaxError: Cannot use import statement outside a module" When Importing ReactJS 未捕获的语法错误:导入 React 时无法在模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module" When Importing React 导入 ECMAScript 6 时出现“Uncaught SyntaxError: Cannot use import statement outside a module” - "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 导入模块时不能在模块外使用 import 语句 - Cannot use import statement outside a module when I import a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM