Angular2-根相对进口 [英] Angular2 - root relative imports

查看:67
本文介绍了Angular2-根相对进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在angular2/typescript中导入有问题.我想使用带有诸如"app/components/calendar"之类的根目录的导入,相反,我只能使用的方式是:

I have a problem with imports in angular2/typescript. I'd like to use imports with some root like 'app/components/calendar', instead only way I am able to use is something like:

//app/views/order/order-view.ts
import {Calendar} from '../../components/calendar 

其中日历"的定义如下:

where Calendar is defined like:

//app/components/calendar.ts
export class Calendar {
}

显然,随着层次的降低,这种情况会变得更糟,最深的是"../../..",但它仍然非常糟糕且脆弱.有什么方法可以使用相对于项目根目录的路径吗?

and this obviously gets much worse the lower in hierarchy you go, deepest is '../../..' but it is still very bad and brittle. Is there any way how to use paths relative to project root?

我正在Visual Studio中工作,相对导入似乎是使VS能够识别这些导入的唯一方法.y

I am working in Visual Studio, and relative imports seem to be the only thing that makes VS able to recognize these imports.y

推荐答案

更新

请不要使用此解决方案.拥抱节点模块解析算法. 社区依靠它,因此,如果您尝试这样做,一切都会崩溃.使用别名或其他提供的解决方案.

UPDATE

Just don't use this solution. Embrace node module resolution algorithm. Community rests on it, so everything will break apart if you try to do otherwise. Use aliases or some of the other provided solutions.

简短答案 有一种方法,但是您不应该这样做. 将compileerOption "moduleResolution"设置为"classic".

Short answer There's a way but you shouldn't do it. Set the compilerOption "moduleResolution" to "classic".

好答案

您使用的是tsconfig.json吗?我想你是.我一直在寻找一种将诸如import someModule = require ("../../../../someModule"之类的语句转换为import someModule=require("src/path/to/someModule")的方法. 数小时的浪费之后,我发现tsc可能会使用不同的算法进行模块解析.我使用的是atom,它使用compilerOption属性moduleResolution设置为"node"的方式创建了tsconfig.json,并且它使用了节点的糟糕的模块解析算法.我只是把经典"放在并开始以明显的方式工作.

Are you using tsconfig.json? I assume you are. I've been looking a way to make statements such as import someModule = require ("../../../../someModule" into import someModule=require("src/path/to/someModule"). I found after hours wasted that tsc may use different algorithms for module resolution. I'm using atom and it creates the tsconfig.json with the compilerOption property moduleResolution set to "node" and it uses the shitty (excuse my french) module resolution algorithm of node. I just put "classic" and started working the obvious way.

这篇关于Angular2-根相对进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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