我有这样的项目结构:
- MyApp
`- firstFolder
`- firstFile.js
`- secondFolder
`- thirdFolder
`- thirdFile.js
如何导入
firstFile.js
来自 thirdFile.js
?像
import myFunc from '../firstFolder/firstFile';
在 thirdFile.js
,不起作用。
最佳答案
描述
以下是路径选项:../
将返回 1 个文件夹,这就是我们返回两个文件夹的原因:
import myFunc from ../../firstFolder/firstFile
所以..
带我们回到 secondFolder
那么我们需要再返回一个文件夹..
进入MyApp
文件夹。现在我们可以向前遍历到 firstFolder
然后指向firstFile
.或
./
- 这是当前工作目录 (pwd),将是 thirdFolder
从这里我们需要返回 2 个目录或 ../..
import myFunc from ./../../firstFolder/firstFile
其他目录路径选项:由于您没有指定完整路径,因此这些路径不完整
/
- 将从操作系统的根目录开始import myFunc from /fullPathtoProject/MyApp/firstFolder/firstFile
~/
这是当前用户的主目录import myFunc from ~/pathFromHomeDirectory/MyApp/firstFolder/firstFile
关于javascript - 基于 JavaScript 中的相对路径导入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42209645/