简体   繁体   English

如何使 Visual Studio Code 使用路径映射进行自动导入?

[英]How to make Visual Studio Code use path mappings for automatic imports?

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我在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.我现在可以使用import {... } from '@myapp/...'这样的工作导入,但从 Visual Studio Code 自动导入仍然是相对的。

I tried this in the settings.json (as written in https://blog.angulartraining.com/path-mapping-with-typescript-cac16d12c416 )我在settings.json中尝试了这个(如https://blog.angulartraining.com/path-mapping-with-typescript-cac16d12c416中所写)

"editor.codeActionsOnSave": {
    "source.organizeImports": true
}

But it didn't help.但这没有帮助。 How to make Visual Studio Code's automatic imports use the paths mapping.如何使 Visual Studio Code 的自动导入使用路径映射。 Also, is there a tool that would help me refactor all of my code?另外,是否有工具可以帮助我重构所有代码?

  1. Navigate to File>Preferences>Settings>User Settings on VSCode 导航到VSCode上的File>Preferences>Settings>User Settings

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

If you have both - mapped paths & relative - after PR #40637 you can use如果您同时拥有 - 映射路径和相对路径 - 在PR #40637之后,您可以使用

"typescript.preferences.importModuleSpecifier": "shortest"

暂无
暂无

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

相关问题 如何在 Visual Studio Code 中使用 TypeScript 和 JavaScript 禁用自动导入的自动分号? - How to disable automatic semicolon for auto imports with TypeScript and JavaScript in Visual Studio Code? Intellisense无法在Visual Studio Code中使用导入 - Intellisense not working with imports in Visual Studio Code 如何在Visual Studio代码中从CSS自动导入类名? - How to automatic import a classname from css in visual studio code? 如何在 Visual Studio Code 中因缺少导入而突出显示错误? - How can I get error highlighting for missing imports in Visual Studio Code? 获取Visual Studio代码以为自定义Javascript导入提供智能感知 - Get Visual Studio Code to give intellisense for custom Javascript imports 如何在 Visual Studio 代码扩展中获取项目文件夹路径 - How to get project folder path in visual studio code extension 如何让 Visual Studio Code 检查整个项目是否有错误? - How to make Visual Studio Code check entire project for errors? 如何在Visual Studio 2008的.ASPX页中的Javascript代码中使用断点 - How to use breakpoints in Javascript code in .ASPX page of Visual Studio 2008 如何使用正则表达式在 Visual Studio Code 的代码片段中“大写和替换”? - How to use Regex to 'capitalize and replace' in Visual Studio Code's snippets? 如何在带有 webview 的 visual studio 代码扩展中使用 axios - How to use axios in visual studio code extensions with webview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM