简体   繁体   English

Angular 10:尝试导入模块时找不到名称

[英]Angular 10: Cannot find name when trying to import module

I created a new Angular project and a new module inside that project using the CLI.我使用 CLI 创建了一个新的 Angular 项目和该项目中的一个新模块。 If I try to import the newly created module in app.module.ts the Intellisense doesn't work and it can't find the module to auto-import it.如果我尝试在app.module.ts中导入新创建的模块,则 Intellisense 不起作用,它找不到要自动导入它的模块。 I didn't change anything inside the tsconfig.json files.我没有更改tsconfig.json文件中的任何内容。

  • Angular CLI: 10.0.4 Angular CLI:10.0.4
  • Node: 12.16.3节点:12.16.3
  • Angular: 10.0.5 Angular:10.0.5
  • Typescript: 3.9.7 Typescript:3.9.7
  • VS Code: 1.47.3 VS 代码:1.47.3

Any idea what could cause it?知道是什么原因造成的吗?

EDIT: A manual import works fine, but I'm asking about the auto-import functionality.编辑:手动导入工作正常,但我问的是自动导入功能。

在此处输入图像描述

在此处输入图像描述

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';



@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class TestModule { }

I found the source of the problem in the tsconfig.json inside the project's root folder.我在项目根文件夹中的tsconfig.json中找到了问题的根源。

{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
]
}

If you just change it to this如果你只是把它改成这个

{
  "extends": "./tsconfig.base.json"
}

VS Code auto-import starts working VS Code 自动导入开始工作

As I understand it the main problem is "files": [], typescript stops seeing all files that are not included in the project.据我了解,主要问题是“文件”:[],typescript 停止查看项目中未包含的所有文件。

github.com/microsoft/TypeScript/issues/39632 github.com/microsoft/TypeScript/issues/39632
https://github.com/angular/vscode-ng-language-service/issues/876 https://github.com/angular/vscode-ng-language-service/issues/876
People offer the same solution人们提供相同的解决方案

Try exporting child components inside Testmodule like this尝试像这样在 Testmodule 中导出子组件

 @NgModule({
      declarations: [],
      imports: [CommonModule],
      exports: [components]
    })
export class TestModule { }

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

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