在打字稿中导入模块时,绝对路径的根源是什么? [英] What is the root of an absolute path when importing a module in typescript?

查看:111
本文介绍了在打字稿中导入模块时,绝对路径的根源是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用打字稿开发应用程序(在Visual Studio 2015中),并且具有以下基本文件结构:

I'm developing an app in typescript (in Visual Studio 2015) and have this basic file structure:

Solution
    AppProject
        Scripts
            framework
                Utils.ts
            app
                SomeApp.ts
        tsconfig.json

现在在应用程序模块中,我想使用绝对路径引用框架模块,因此我将执行以下操作:

Now within the app modules, I would like to reference the framework modules with an absolute path, so I would do something like this:

import { Utils } from '/Scripts/framework/Utils'

但是这不起作用.
我收到红色的波浪线和"找不到模块'/Scripts/framework/Utils'"

However this doesn't work.
I'm getting the red squiggly line and a "Cannot find module '/Scripts/framework/Utils'"

在执行相对路径时,我可以很好地工作,但是该应用显然比所示的要复杂,并且我不想为自己的相对路径走出多个层次.

I works fine when doing a relative path, but the app is obviously more complex than what is shown, and I don't want to deal with stepping out multiple levels for my relative path.

我正在将Typescript 1.8与节点模块解析策略一起使用.
然后通过webpack对其进行编译和捆绑

I am using typescript 1.8 with the node module resolution strategy.
And webpack to compile and bundle it, if that matters

推荐答案

很遗憾,我在文档中找不到任何内容.因此,在尝试解析绝对路径的导入时,我跟踪了由编译器(TypeScript 2.5.2)执行的文件系统访问.

Unfortunately I couldn't find anything on that in the docs. So I traced the file system accesses performed by the compiler (TypeScript 2.5.2) when trying to resolve an import of an absolute path.

结果是 /始终引用文件系统根目录.不管compilerOptions(例如rootDirrootDirsbaseUrl).

The result is that / always refers to the file system root. Regardless of compilerOptions like rootDir, rootDirs or baseUrl.

这篇关于在打字稿中导入模块时,绝对路径的根源是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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