简体   繁体   English

如何使用ES6模块导入导入路径

[英]How to import path using ES6 module import

Is there any good way to load a module elegantly (where the IDE can suggest or go into the file) using dynamic path or start from the root directory to import a module? 有没有什么好方法可以优雅地加载模块(IDE可以建议或进入文件)使用动态路径或从根目录开始导入模块?

 import * as Q from 'q'; import * as loopback from 'loopback'; import datasources from '../../../datasources.json'; import app from '../../../server'; import {ApiError, ValidationError, DatabaseError} from'../../../utils/error-handlers'; 

The module identifier is not necessarily even a path at all. 模块标识符甚至根本不是路径。 For example, it often searches the node_modules folder. 例如,它经常搜索node_modules文件夹。 The quick answer is the syntax depends on the module loader. 快速回答是语法取决于模块加载器。

But the pragmatic answer, if you are using Babel (as you might be since you specified this was an ES6 import), is that you can specify a location relative to the project root with an '@' at the beginning. 但是实用的答案,如果你使用Babel(因为你指定这是一个ES6导入,你可能就是这样),你可以在开头用一个'@'指定相对于项目根目录的位置。 For example, in my Vue.js project, I have: 例如,在我的Vue.js项目中,我有:

import PageFooter from '@/components/PageFooter'

In my case, this can avoid issues of having a complex tree of nested components, at various depths. 在我的例子中,这可以避免在不同深度处具有嵌套组件的复杂树的问题。

In your case, you could have: 在您的情况下,您可以:

import app from '@/server'

assuming server was in the project root (eg as server.js and not a folder itself). 假设服务器位于项目根目录中(例如,作为server.js而不是文件夹本身)。

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

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