根据JavaScript中的相对路径导入文件 [英] Import file based on relative path in JavaScript

查看:321
本文介绍了根据JavaScript中的相对路径导入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的项目结构:

- MyApp
`- firstFolder
 `- firstFile.js
`- secondFolder
 `- thirdFolder
  `- thirdFile.js

如何从 thirdFile.js 导入 firstFile.js

类似于从'../firstFolder/firstFile'导入myFunc; thirdFile.js 中,不起作用。

Something like import myFunc from '../firstFolder/firstFile'; in thirdFile.js, doesn't work.

推荐答案

说明



../将返回1个文件夹,这就是为什么我们返回两个文件夹的原因:

Description

../ will go back 1 folder, this is why we go back two folders:

import myFunc from ../../firstFolder/firstFile.js; 

否则它们将从根目录开始

or these will start at the root directory

import myFunc from /firstFolder/firstFile.js
import myFunc from ./firstFolder/firstFile.js
import myFunc from ~/firstFolder/firstFile.js

这篇关于根据JavaScript中的相对路径导入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆