我正在尝试将 editor.js 安装到我的项目中,但是,我不断遇到错误
Uncaught SyntaxError: Cannot use import statement outside a module
。
我可以通过 CDN 加载它,但由于某种原因,这无法工作。
以下是我迄今为止完成的故障排除步骤:
"type":"module"
添加到 JSON 文件type="module"
添加到普通 js 脚本我已经通过 stackoverflow 进行了搜索,但找不到除这些之外的解决方案。有什么建议吗?
HTML 文件:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="editorjs"></div>
<script src="index.js"></script>
</body>
</html>
JS 文件:
import EditorJS from '@editorjs/editorjs';
const editor = new EditorJS();
打包 JSON 文件:
{
"name": "editor",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@editorjs/editorjs": "^2.18.0"
}
}
如果您从 CDN 获取 editor.js,则不需要 import 语句。在你的 JS 文件中你需要的是:
const editor = new EditorJS();
我自己也摔倒了。我认为 Editor.js 文档here具有误导性。它首先说有 3 种安装 editor.js 的方法,但接下来的所有示例都假设您已经使用了
npm
。