简体   繁体   English

Angular2 - WebStorm 通过 index.ts 导入我的模块

[英]Angular2 - WebStorm import my modules by index.ts

Scenario场景

I'm currently working on an Angular2 app which is growing in complexity.我目前正在开发一个越来越复杂的 Angular2 应用程序。 To manage this, we're dividing the app into Angular modules.为了管理这一点,我们将应用程序划分为 Angular 模块。

Problem问题

When I use a TypeScript class from one module in another Angular module, WebStorm automatically references the complete file location.当我在另一个 Angular 模块中的一个模块中使用 TypeScript 类时,WebStorm 会自动引用完整的文件位置。 I get this every time I press the Tab key每次按Tab键时我都会得到这个

// main.component.ts
import {class1} from "../otherModule/folder/class1";
import {class2} from "../otherModule/folder/class2";

Here's the directory structure, each folder contains an index barrel which references the folders within it.这是目录结构,每个文件夹都包含一个索引桶,引用其中的文件夹。

mainModule/
-main.component.ts
-main.module.ts

otherModule/
-folder/
--index.ts
--class1.ts
--class2.ts
-index.ts
-other.module.ts

Ideal solution理想的解决方案

Ideally I would like to have WebStorm automatically only reference the module name when I press the Tab key.理想情况下,我希望 WebStorm 仅在按Tab键时自动引用模块名称。 So that I can change the location of the class within the module without worrying about dependent references.这样我就可以在模块中更改类的位置,而不必担心依赖引用。

import {class1, class2} from "../otherModule";

This functionality seems to be possible, as it works with the Angular core features and other npm modules.这个功能似乎是可能的,因为它可以与 Angular 核心功能和其他 npm 模块一起使用。

import {Component, OnInit, OnDestroy} from "@angular/core";

Any help pointing me in the right direction is much appreciated.非常感谢为我指明正确方向的任何帮助。

From answer http://stackoverflow.com/a/40637423/1057218从答案http://stackoverflow.com/a/40637423/1057218

You need to open File |您需要打开文件 | Settings |设置 | Editor |编辑 | Code Style |代码风格 | Typescript (Imports tab) settings and enable the option [Use directory import] Typescript(导入选项卡)设置并启用选项[使用目录导入]

Note: requires WebStorm 2017.2 or higher注意:需要WebStorm 2017.2或更高版本

在此处输入图片说明

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

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