简体   繁体   English

错误的自动导入路径别名

[英]Faulty auto import with path alias

I'm having strange issue when auto importing services/components from folder provided with path alias within Angular 9 application.从 Angular 9 应用程序中提供路径别名的文件夹中自动导入服务/组件时,我遇到了奇怪的问题。

Those are my aliases defined in tsconfig.json这些是我在tsconfig.json 中定义的别名

"paths": {
  "@core/*": ["app/core/*"],
  "@shared/*": ["app/shared/*"],
  "@state/*": ["app/state/*"]
}

Then in Core folder I'm reexporting all services in index file ( app/core/index.ts )然后在 Core 文件夹中,我app/core/index.ts索引文件( app/core/index.ts )中的所有服务

export * from './sse/sse.service';
export * from './rack/rack.service';

Now when I'm injecting the service in a constructor the provided auto import option gets imported with faulty path:现在,当我在构造函数中注入服务时,提供的自动导入选项会以错误的路径导入:

// Incorrect - auto imported path
import { RackService } from '@core/';

// Correct path after manual fix
import { RackService } from '@core/index';

It's just a minor issue but quite annoying at the same time and I'm not sure whether it's some incorrect configuration on my side, or VS Code issue.这只是一个小问题,但同时也很烦人,我不确定这是我这边的配置不正确,还是 VS Code 问题。 Any idea?任何的想法? Could it be caused by JEST as I'm using it instead of Jasmine and it requires to have aliases defined in package.json also.它可能是由 JEST 引起的,因为我正在使用它而不是 Jasmine,它也需要在package.json定义别名。

  "jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "./src/setup-jest.ts"
    ],
    "moduleNameMapper": {
      "^@core/(.*)$": "<rootDir>/src/app/core/$1",
      "^@shared/(.*)$": "<rootDir>/src/app/shared/$1",
      "^@state/(.*)$": "<rootDir>/src/app/state/$1"
    }
  },

Seems like the issue is caused by the first alias: @core/* which can't be then resolved properly in case that the export file is directly on the first level.似乎问题是由第一个别名引起的: @core/*如果导出文件直接位于第一级,则无法正确解决。 I came up with two workarounds:我想出了两个解决方法:

1 - Move the index file to a folder named services which contains only the index.ts . 1 - 将索引文件移动到仅包含index.ts名为services的文件夹中。 Now when auto importing (or TS Hero: Organizes imports) the auto import looks like this:现在,当自动导入(或 TS Hero:组织导入)时,自动导入如下所示:

import {someService} from '@core/services'

2 - Add path alias to the tsconfig.*.json files just for the services pointing directly to re-export index.ts 2 - 为直接指向重新导出index.ts的服务添加路径别名到tsconfig.*.json文件

"paths": {
  "@core/*": ["app/core/*"],
  "@core/services": ["app/core/index.ts"],
  "@shared/*": ["app/shared/*"],
  "@state/*": ["app/state/*"]
}

Second option also requires changes in package.json if you are using Jest framework.如果您使用 Jest 框架,则第二个选项还需要在package.json中进行更改。

"moduleNameMapper": {
  "^@core/services$": "<rootDir>/src/app/core/index.ts",
  "^@core/(.*)$": "<rootDir>/src/app/core/$1",      
  "^@shared/(.*)$": "<rootDir>/src/app/shared/$1",
  "^@state/(.*)$": "<rootDir>/src/app/state/$1"
}

In both cases, if you have any other re-export index.ts deeper in the core folder it will still be used correctly, eg @core/commons在这两种情况下,如果您在核心文件夹中更深处有任何其他重新导出index.ts ,它仍将被正确使用,例如@core/commons

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

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