简体   繁体   English

在项目中手动导入之前,VSCode IntelliSense 不建议使用 Angular 模块

[英]VSCode IntelliSense doesn't suggest Angular modules until imported manually in the project

First you have to understand that I'm switching from IntelliJ to VSCode (Maybe I was too comfortable the way IntelliJ was looking for modules).首先,您必须了解我正在从 IntelliJ 切换到 VSCode(也许我对 IntelliJ 寻找模块的方式太熟悉了)。

I'll mostly use VSCode for developing Angular app generated by the Angular CLI.我将主要使用 VSCode 来开发由 Angular CLI 生成的 Angular 应用程序。

Starting from a new CLI generated app, open the project in a fresh VSCode installation (removed the settings and all extensions) I realized that I need to manually import any Angular modules before VSCode IntelliSense getting aware of them.从一个新的 CLI 生成的应用程序开始,在新的 VSCode 安装中打开项目(删除了设置和所有扩展)我意识到我需要在 VSCode IntelliSense 意识到它们之前手动导入任何 Angular 模块。

Example: Let say I need to create 2 Angular services.示例:假设我需要创建 2 个 Angular 服务。 Both will require HttpClient to fetch data.两者都需要 HttpClient 来获取数据。 On the first one, I change the constructor properties to inject it.在第一个中,我更改构造函数属性以注入它。 When pressing the Quick Fix shortcut (CTRL + .) I'm getting only one suggestion which is to import the HttpClient from selenium-webdriver.当按下 Quick Fix 快捷方式 (CTRL + .) 时,我只得到一个建议,即从 selenium-webdriver 导入 HttpClient。

在此处输入图片说明

Still in the first service, I added Angular HttpClient import manually:还是在第一个服务中,我手动添加了Angular HttpClient import:

import { HttpClient } from '@angular/common/http';

Going to the second service, change the constructor to add the httpClient property the same way and now I'm getting much more results when pressing the Quick Fix shortcut:转到第二个服务,更改构造函数以相同的方式添加 httpClient 属性,现在按 Quick Fix 快捷方式时我得到了更多结果:

在此处输入图片说明

Is that the expected behavior or I got something wrong?这是预期的行为还是我做错了什么? Already tried this on 3 different systems and same result.已经在 3 个不同的系统上尝试过这个,结果相同。 I was expecting the IntelliSense a bit remove intelligent about the HttpClient context.我期待 IntelliSense 对 HttpClient 上下文有一点删除智能。

This is the same for any modules, meaning you need to manually import once before VSCode getting aware of it.这对于任何模块都是一样的,这意味着您需要在 VSCode 意识到它之前手动导入一次。

The only solution I've found so far is adding the path to Angular packages into typeRoots of tsconfig.json .到目前为止,我找到的唯一解决方案是将 Angular 包的路径添加到typeRootstsconfig.json Here's an example:下面是一个例子:

"typeRoots": ["node_modules/@types", "node_modules/@angular"]

That way, apart from node_modules/@types , TypeScript looks into the node_modules/@angular folder and its subfolders for type definitions.这样,除了node_modules/@types ,TypeScript node_modules/@angular查看node_modules/@angular文件夹及其子文件夹中的类型定义。

According to the TypeScript docs :根据打字稿文档

If typeRoots is specified, only packages under typeRoots will be included.如果指定了typeRoots ,则只包含typeRoots下的包。

I don't actually know why nothing was mentioned on that matter in the Angular documentation , which is correct but sounds a bit incomplete in terms of typing discovery:我实际上不知道为什么Angular 文档中没有提及此事,这是正确的,但在键入发现方面听起来有点不完整:

Many libraries include definition files in their npm packages where both the TypeScript compiler and editors can find them.许多库在它们的 npm 包中包含定义文件,TypeScript 编译器和编辑器都可以在其中找到它们。 Angular is one such library. Angular 就是这样一个库。 The node_modules/@angular/core/ folder of any Angular application contains several d.ts files that describe parts of Angular.任何 Angular 应用程序的 node_modules/@angular/core/ 文件夹都包含几个描述 Angular 部分的 d.ts 文件。

You don't need to do anything to get typings files for library packages that include d.ts files.您无需执行任何操作即可获取包含 d.ts 文件的库包的类型文件。 Angular packages include them already. Angular 包已经包含了它们。

I reproduced, understood and solved this problem and actually to have the httpClient stuff in the hints, you've to add also the node_modules/@angular/common/http .我复制、理解并解决了这个问题,实际上要在提示中包含 httpClient 内容,您还必须添加node_modules/@angular/common/http This has to do with the way they export the type definitions, you can find my issue that you can upvote with further information on why it happens exactly and shortly a PR to solve this here https://github.com/angular/angular/issues/35237这与他们导出类型定义的方式有关,您可以找到我的问题,您可以通过进一步的信息投票支持它为什么会发生,并且很快就可以在这里解决这个问题https://github.com/angular/angular/问题/35237

Anyway, to reproduce this you can also just head over to stackblitz and try to input "httpc" and you'll see no suggestion.无论如何,要重现这一点,您也可以前往 stackblitz 并尝试输入“httpc”,您将看不到任何建议。

Yes you are correct, i have faced this many times during building demos for workshops. 是的,您是对的,我在为研讨会制作演示时遇到过很多次。 Problem was with Visual Studio Code's Autoimport plugin importing HttpClient class from 问题是Visual Studio Code的Autoimport插件Autoimport导入HttpClient

'../../node_modules/@types/selenium-webdriver/http'

instead of 代替

'../../node_modules/@angular/common/http'

Mentioning @Andriy Chuma's answer, what worked for me was adding提到@Andriy Chuma 的回答,对我有用的是添加

"node_modules/@angular/common/http"

at the end of在......的最后

compilerOptions block compilerOptions

in tsconfig.json :tsconfig.json

"typeRoots": [
  "node_modules/@types",
  "node_modules/@angular",
  "node_modules/@angular/common/http"
]

Now VSCode intellisense autocompletes whenever I try to import HttpClientModule into any file现在,每当我尝试将HttpClientModule导入任何文件时,VSCode 智能感知都会自动完成

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

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