2

我有 3 个文件一个 HTML,2 个名为 app.js 和 customer .js 的 javascript 文件

我有一个 HTML 页面,我在上面写了这个

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    hello
    <script src="app.js" type="module"></script>
</body>
</html>   

我刚刚将 app.js 加载到此页面中

app.js 包含这个

import {person} from "./customer";
console.log("helllo");

在 customer.js 我有这个

const person={
    name:"hello"
}

export default person;

我收到错误显示的导入错误

GET http://127.0.0.1:5500/customer net::ERR_ABORTED 404 (Not Found)

我是Web开发的新手,请帮助我。

4

1 回答 1

1

在这里,您导入它的方式是错误的。

让我解释。首先,我可以看到您默认导出模块或其他任何内容。在这种情况下,您不需要那些大括号。

import person from "./customer.js";

其次,只需确保在您的设置中package.json您已"type"设置为"module"这样

"type": "module",

第三,如果您使用的是VSCode,那么您可以右键单击您的customer.js文件并单击copy relative path以从您的项目中获取其相对路径。

概括:

  1. 更正您的导入语句
  2. 检查 package.json 是否正确
  3. 使用 VSCode 复制相对路径

希望这有助于解决您的查询!

于 2021-09-16T17:46:12.317 回答