我已经为这个问题苦苦挣扎了好几天。问题是导入效果不佳,我不知道为什么。这里有些例子:
我有一个 index.html,完全是空的,我只有脚本。像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module" src="/src/index.js"></script>
</body>
</html>
在 src 文件夹中,我有 index.js 和 app.js。在 app.js 中的代码是:
const helloWorld = () => {
console.log('Hello world');
};
export default helloWorld;
在 index.js 中的代码是:
import helloWorld from 'app.js';
helloWorld()
我使用VSC Live Server扩展挂载服务器,Chrome控制台说:
未捕获的类型错误:无法解析模块说明符“app.js”。相对引用必须以“/”、“./”或“../”开头。
我说:“好的”,然后在 index.js 中尝试:
import helloWorld from './app';
helloWorld()
Chrome 控制台说:
获取http://127.0.0.1:5500/src/app net::ERR_ABORTED 404(未找到)
这个问题也发生在我的 node_modules 中的所有 npm 包中。例子:
import bootstrap from 'bootstrap';
import axios from 'axios';
铬控制台:
未捕获的类型错误:无法解析模块说明符“引导程序”。相对引用必须以“/”、“./”或“../”开头。
未捕获的类型错误:无法解析模块说明符“axios”。相对引用必须以“/”、“./”或“../”开头。
VSC 可以识别 axios 和引导程序路由,但是在浏览器中尝试时会出现问题。例子:
我也尝试使用另一台本地服务器,但它也不起作用。我尝试了这个链接中解释的那个:
要添加的其他内容是它说错误在 index.html 的第 1 行:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句