简体   繁体   English

Visual Studio Code 无法解析角度的 tsconfig 路径

[英]Visual Studio Code can't resolve angular's tsconfig paths

I'm trying to import some services using barrels and tsconfigs paths options but I can't get angular and vscode to get along.我正在尝试使用桶和 tsconfigs paths选项导入一些服务,但我无法让 angular 和 vscode 相处。

If it works for one it doesn't for the other and viceversa...如果它对一个有效,则对另一个无效,反之亦然...

My situation seems to be pretty simple:我的情况似乎很简单:

  • in src/app/services I have a service which is exported in a index.tssrc/app/services我有一个在 index.ts 中导出的服务
  • my src/tsconfig.app.json is just this:我的src/tsconfig.app.json就是这样的:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": [],
    "baseUrl": ".",
    "paths": {
      "services": ["app/services"]
    }
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ],
}

and my angular app compiles with no issues , but vscode keep giving me errors every time I try to import my service from 'services' giving me [ts] Cannot find module 'services'.我的 angular 应用程序编译没有问题,但是每次我尝试从'services'导入我的服务时,vscode 总是给我错误,给我[ts] Cannot find module 'services'.

why?为什么?

I'm using typescript 3.1.6 and in vscode settings I have "typescript.tsdk": "C:/Users/myuser/AppData/Roaming/npm/node_modules/typescript/lib" (I also tried to leave the default setting, no changes)我正在使用 typescript 3.1.6 并且在 vscode 设置中我有"typescript.tsdk": "C:/Users/myuser/AppData/Roaming/npm/node_modules/typescript/lib" (我也尝试保留默认设置,没有变化)


edit:编辑:

if I specify paths in ./tsconfig.json starting from src , vscode is happy but angular is not.如果我在./tsconfig.json中指定从src开始的paths ,vscode 是快乐的,但 angular 不是。

if I specify paths in both tsconfig.json and src/tsconfig.app.json , both vscode and angular are happy, but it seems a too stupid workaround to me...如果我在tsconfig.jsonsrc/tsconfig.app.json中指定paths ,vscode 和 angular 都很高兴,但这对我来说似乎是一个太愚蠢的解决方法......

I figured it out, even if I'm keep thinking that is all absurd... 我想通了,即使我一直认为那都是荒谬的...

VsCode automatically looks for a tsconfig.json file and it doesn't care about tsconfig.app.json , so paths needs to be specified in tsconfig.json . VsCode自动查找tsconfig.json文件,而不关心tsconfig.app.json ,因此需要在tsconfig.json指定paths

At the same time, the angular-cli scaffolding specify a baseUrl parameter in tsconfig.app.json which overrides the upper one. 同时,角度-CLI脚手架指定baseUrl在参数tsconfig.app.json它覆盖上面的一个。

The solution is to delete baseUrl parameter in the tsconfig.app.json or edit its value to "../" 解决的办法是删除baseUrl参数在tsconfig.app.json 编辑其值设置为"../"

(As a personal remark, given that vscode is largely used to build angular solutions, I think that there's something that should be revisited in the angular-cli scaffolding or in how vscode looks for tsconfig files) (作为个人评论,鉴于vscode主要用于构建角度解决方案,我认为应在Angle-cli脚手架或vscode查找tsconfig文件的方式中进行一些修改)

this answer worked for me.这个答案对我有用。

  1. Open Command Palette and select TypeScript: select TypeScript Version ...打开命令面板并选择TypeScript: select TypeScript Version ... 在此处输入图片说明

  2. Select to use Workspace Version选择使用工作区版本在此处输入图片说明

Hope this answer solves problem of others facing the same problem.希望这个答案可以解决其他面临同样问题的人的问题。

Another possible reason of issues with resolving aliases can be caused by using include property in main tsconfig file.解决别名问题的另一个可能原因是在主 tsconfig 文件中使用 include 属性。 Default value is ['**/*'] - just add '**/*' to the included paths when you override it.默认值为['**/*'] - 覆盖时只需将 '**/*' 添加到包含的路径中。 This was my problem, but it was hard to detect that include is causing this.这是我的问题,但很难检测到包含导致了这个问题。

  1. Create tsconfig.json in your application near tsconfig.app.json .创建tsconfig.json在靠近你的应用程序tsconfig.app.json
  2. Add next settings添加下一个设置
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
       /* your_paths */
    }
  }
}
  1. In your tsconfig.app.json and tsconfig.spec.json rewrite on "extends": "./tsconfig.json"在你的tsconfig.app.jsontsconfig.spec.json重写"extends": "./tsconfig.json"

Final file structure最终文件结构

projects
  your_application
    tsconfig.json
    tsconfig.spec.json
    tsconfig.app.json
tsconfig.json

I was struggling with this issue for a long time and have tried just about every solution on the inte.net.我为这个问题苦苦挣扎了很长时间,并尝试了 inte.net 上的几乎所有解决方案。 The project would compile but VSCode would not honor the "paths" declarations in my tsconfig.json.该项目可以编译,但 VSCode 不会遵守我的 tsconfig.json 中的“路径”声明。

What finally fixed it for me was removing the "include" and "exclude" sections of my tsconfig.json最终为我修复的是删除我的 tsconfig.json 的“包含”和“排除”部分在此处输入图像描述

I'm not sure which one of the 2 was the problem, but removing both finally fixed my long-standing issue.我不确定这两个问题中的哪一个是问题,但删除两者最终解决了我长期存在的问题。

I encountered a similar situation with Visual Code and Angular 7 我在Visual Code和Angular 7中遇到了类似情况

ORIGINAL CODE (valid path, from a book example): 原始代码(有效路径,来自书籍示例):

import { Stock } from 'app/model/stock';

ERROR: 错误:

Failed to compile.
./src/app/app.component.ts Module not found: Error: Can't resolve 'app/model/stock'...

"CORRECTED" CODE: “正确的”代码:

import { Stock } from './model/stock';

It seems VSCode only checks the tsconfig.json directly in the folder your open . 似乎VSCode 在打开的文件夹中直接检查tsconfig.json It does not check folders above like tsc and does not look at tsconfig.app.json . 它不会像tsc一样检查上方的文件夹,也不会查看tsconfig.app.json So in some cases the baseUrl might be missing because VSCode simply has not read a tsconfig.json . 因此,在某些情况下baseUrl可能会丢失,因为VSCode根本没有阅读tsconfig.json

VSCode's limitation to a single tsconfig.json in a fixed place has been around for a while and it seems it's not so easy to make this more flexible: https://github.com/microsoft/vscode/issues/12463 (scroll to end) VSCode的限制单一tsconfig.json在一个固定的地方已经有一段时间了,似乎它不是那么容易使这种更灵活: https://github.com/microsoft/vscode/issues/12463 (滚动结束)

As mentioned above this is annoying in Angular-CLI scaffolding with multiple sub-projects where there is only one tsconfig.json at the root of the workspace. 如上所述,这在具有多个子项目的Angular-CLI脚手架中很烦人,在该tsconfig.json中,工作区的根部只有一个tsconfig.json Obviously the projects might have different baseUrls in their tsconfig.app.json which cannot all go in a single tsconfig.json at the root. 显然,项目可能有不同baseUrls在他们tsconfig.app.json不能都去在单一tsconfig.json在根。

As a workaround I have in addition to the project specific baseUrl in tsconfig.app.json created an extra minimal tsconfig.json for VSCode in the src folder of a sub-project with "baseUrl":"." 作为一种变通方法我在除了特定项目baseUrltsconfig.app.json创建一个额外的最小tsconfig.json在一个分项目与src文件夹中的VSCode "baseUrl":"." and open VSCode from there. 然后从那里打开VSCode。 I have not tested this severely but so far Angular compiling and VSCode intellisense both seem to be happy. 我没有对此进行严格的测试,但是到目前为止,Angular编译和VSCode intellisense似乎都很满意。

在你的项目工作角的情况下,让地方baseUrlpathstsconfig.json而不是tsconfig.app.json

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

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