从深度嵌套的目录结构导入模块 [英] Importing modules from deeply nested directory structures
问题描述
导入不在同一文件夹或其周围的模块是非常麻烦的。你必须继续计算'../'。就像下面的例子一样:
Importing a module that is not in or around the same folder is quite a nuisance. You have to keep counting the '../'. Like in the example below:
import {AnswersService, AddAnswerModel, Answer} from '../../../../../../../BackendServices/AnswersService';
通过将我的System.config修改为下面的示例,我可以绕过所有这些。 ./',代码在浏览器上完美工作。
By modifying my System.config to the example below, I can get around having all of these '../' and the code works perfectly on the browser.
System.config({
packages: {
'app': { defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' }
},
paths: {
'rxjs/*': 'node_modules/rxjs/*',
'BackendServices/*': 'app/BackendServices/*'
}
});
它将import语句减少到以下可管理命令。
It reduces the import statement to the manageable command below.
import {AnswersService, AddAnswerModel, Answer} from 'BackendServices/AnswersService';
但是,这种方法的问题是我在Visual Studio代码中失去了智能感知。我不知道这是一个typcript问题,一个可视化的工作室代码问题,还是别的什么。
But the problem with this approach is that I lose intellisense in Visual Studio Code. I am not sure if this is a typescript problem, a visual studio code problem, or something else.
有没有人知道如何使这项工作不失智慧?
Does anyone know how to get this working without losing intellisense?
推荐答案
p> Visual Studio只能解析位于node_modules中的相对路径和模块,如 angular2 / *
或 rxjs / *
。
Visual Studio can only resolve relative paths and modules located in node_modules like angular2/*
or rxjs/*
.
它是TypeScript默认模块分辨率(节点)..您可以在tsconfig.json中使用classic进行更改,但VS代码将不再识别节点模块。
It is the TypeScript default moduleResolution ( node ) .. you can change it in tsconfig.json with 'classic' .. but VS Code will no longer recognize node modules.
此问题在此版本 https ://github.com/Microsoft/TypeScript/issues/5039
有很多提案..但还没有实现。
There are many proposals .. but nothing is implemented yet.
这篇关于从深度嵌套的目录结构导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!