最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make Visual Studio Code use path mappings for automatic imports? - Stack Overflow

programmeradmin2浏览0评论

I want to have absolute paths in my project to avoid ugly paths and to have constant imports wherever a file is in the project tree.

I have added this in the tsconfig.json

"paths": {
  "@myapp/*": ["*", "app/*"]
}

I can now have working imports like import { ... } from '@myapp/...' but the automatic imports from Visual Studio Code are still relative.

I tried this in the settings.json (as written in )

"editor.codeActionsOnSave": {
    "sourceanizeImports": true
}

But it didn't help. How to make Visual Studio Code's automatic imports use the paths mapping. Also, is there a tool that would help me refactor all of my code?

I want to have absolute paths in my project to avoid ugly paths and to have constant imports wherever a file is in the project tree.

I have added this in the tsconfig.json

"paths": {
  "@myapp/*": ["*", "app/*"]
}

I can now have working imports like import { ... } from '@myapp/...' but the automatic imports from Visual Studio Code are still relative.

I tried this in the settings.json (as written in https://blog.angulartraining./path-mapping-with-typescript-cac16d12c416)

"editor.codeActionsOnSave": {
    "sourceanizeImports": true
}

But it didn't help. How to make Visual Studio Code's automatic imports use the paths mapping. Also, is there a tool that would help me refactor all of my code?

Share Improve this question asked Sep 2, 2019 at 18:44 Guerric PGuerric P 31.9k6 gold badges58 silver badges106 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10
  1. Navigate toFile>Preferences>Settings>User Settings on VSCode

  2. Set "typescript.preferences.importModuleSpecifier": "non-relative"

If you have both - mapped paths & relative - after PR #40637 you can use

"typescript.preferences.importModuleSpecifier": "shortest"
发布评论

评论列表(0)

  1. 暂无评论