简体   繁体   English

获取Angular 2 .ts文件而不是.d.ts文件

[英]Get Angular 2 .ts files instead of .d.ts files

When I work with angular2 code I often need to see the implementation of a class, let's say the Router class. 当我使用angular2代码时,我经常需要看到一个类的实现 ,比如说Router类。

If I click on the Router type in my IDE WebStorm, eg inside the constructor of another class 如果我在IDE WebStorm中单击Router类型,例如在另一个类的构造函数中

export class myClass {
    constructor(private router: Router) {}
    // ...
}

my IDE takes me to the TypeScript definition file router.d.ts inside my node_modules folder. 我的IDE将我带到node_modules文件夹中的TypeScript定义文件router.d.ts What I want is it to take me to the original router.ts file with the implementation of the router class, not just its definition . 我想要的是带路由器类的实现 ,而不仅仅是其定义 ,将我带到原始的router.ts文件。

The original .ts file is not included in the node_modules folder structure when you get angular2 from github via the standard package.json suggested in the Angular2 Quickstart. 当您通过Angular2快速入门中建议的标准package.json从github获取angular2时,原始的.ts文件不包括在node_modules文件夹结构中。 Currently, I have to look up the original code in the official github repo . 当前,我必须在官方的github repo中查找原始代码。

Any ideas how to get the .ts files into my node_modules/@angular folder instead of the .d.ts files? 任何想法如何获得.ts文件到我的node_modules/@angular文件夹,而不是.d.ts文件?

Sadly, it's not possible since no TS files exist. 遗憾的是,由于不存在TS文件,因此不可能。 Even if you add them it still not possible since you import real angular paths which always point to the definition files. 即使添加它们,也仍然无法实现,因为您将导入始终指向定义文件的真实角度路径。 On top of that the file structure of the project does not correlate to the structure of the import string literals. 最重要的是,项目的文件结构与导入字符串文字的结构不相关。

Some background and more information 一些背景和更多信息

The NPM package does not include .ts files, this is by design from the angular team. NPM软件包不包括.ts文件,这是由角度小组设计的。 Up until some time ago the .ts files were indeed supplied with the NPM package. 直到前一段时间, .ts文件确实与NPM软件包一起提供。

The reasoning for removing them is to disable abuse from users accessing private classes and @internal and private APIs which is public methods/properties in the API that are not supposed to be public but must be so other angular internal classes can use them. 删除它们的原因是禁止用户访问私有类以及@internal和private API的滥用,这些私有类是API中的公共方法/属性,不应被认为是公共的,但必须如此,以便其他角度内部类可以使用它们。

We used to see a lot of code samples out there doing things like import { PromiseCompleter } from 'angular2/src/facade/lang'; 我们以前经常看到很多代码示例,例如import { PromiseCompleter } from 'angular2/src/facade/lang'; (before RC0) but this was changed when the project structure had a big structure refactor in RC0. (在RC0之前),但是当项目结构在RC0中具有较大的结构重构时,这一点已更改。 This abuse was wide and it's bad, very bad... For users and for Angular PR. 这种滥用是广泛的,这是非常糟糕的事情,对用户和Angular PR来说都是如此。

The Angular project has a complex and robust build process where all of the API is moved from .ts files into d.ts files using an automated process that limits exposure. Angular项目具有复杂而强大的构建过程,其中所有API均使用自动过程限制了d.ts范围,从而将所有API从.ts文件移至d.ts文件。 ( public_api_guard ) public_api_guard

The end result is d.ts files only. 最终结果仅是d.ts文件。

It's also not possible to clone the git repo and use it since, again, the file structure is way way different so imports will have to change. 克隆git repo并使用它也是不可能的,因为同样,文件结构是完全不同的,因此导入必须更改。 Most importantly without the build Angular will, most likely, not work. 最重要的是,如果没有构建,Angular很可能无法正常工作。

A solution using a different approach 使用不同方法的解决方案

However, if you debug your app you notice that you reach actual angular core .ts files in the source view of the console, this is because the NPM package comes with source map files that include the whole TS source code. 但是,如果调试应用程序,则会注意到您在控制台的源视图中到达了实际的angular core .ts文件,这是因为NPM软件包随附了包含完整TS源代码的源映射文件。 Nice trick they did there. 他们在那里做的好戏。

This is what I use to dig deep into angular, it works quite great and I get a lot from it. 这是我用来深入了解角度的​​方法,它的效果非常好,并且我从中得到很多。 It's not as nice as Goto Declaration but it something... IMO it's also easier to understand when you step through code... 它不像Goto Declaration那么好,但是它有点... IMO,当您逐步执行代码时也更容易理解...

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

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