简体   繁体   English

index.js 和 index.html 在同一目录下

[英]index.js and index.html in the same directory

I'm trying to learn React and Javascript.我正在尝试学习 React 和 Javascript。 I try to understand how import and export works in ES6, so I put all three files index.html , index.js , and lib.js in the same folder.我试图了解 ES6 中的importexport是如何工作的,所以我将所有三个文件index.htmlindex.jslib.js放在同一个文件夹中。 When I included index.js in my html file, it gave me the error当我在 html 文件中包含index.js时,它给了我错误

Uncaught TypeError: Failed to resolve module specifier "lib". Relative references must start with either "/", "./", or "../".

When I changed the name of index.js to index2.js , everything worked fine.当我将index.js的名称更改为index2.js时,一切正常。 I think the problem is that I was not supposed to put index.html and index.js in the same folder but I don't understand why.我认为问题在于我不应该将index.htmlindex.js放在同一个文件夹中,但我不明白为什么。 Can some please explain?有人可以解释一下吗?

index.html索引.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>
    <h1>Hello World!</h1>
    <script type="module" src="./index.js"></script>
</body>
</html>

index.js index.js

import {square} from './lib.js'

console.log(square(3))

lib.js lib.js

export function square(x){
    return x*x
};

It errors when you link index.js because 2 files can not have the same not name as any other file in the folder even if they have different file extensions.链接 index.js 时会出错,因为 2 个文件不能与文件夹中的任何其他文件同名,即使它们具有不同的文件扩展名。 This is why when you change the name to index2.js it does not error.这就是为什么当您将名称更改为 index2.js 时它不会出错。

It's not really the files having same name in the same folder as the webpage, I found that if you use Firefox instead of Chrome, the problem goes away.这不是与网页在同一文件夹中具有相同名称的文件,我发现如果您使用 Firefox 而不是 Chrome,问题就会消失。 Hope this will help.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM