【问题标题】:Why do I get "Cannot find name 'NgModule'" despite running "npm i @types/node --global"?尽管运行“npm i @types/node --global”,为什么我得到“找不到名称'NgModule'”?
【发布时间】:2019-10-05 01:10:09
【问题描述】:

尽管遵循此处的建议 -- TypeScript error in Angular2 code: Cannot find name 'module',但我仍然收到此错误。我正在使用 Angular 5 和 npm 6.9.0。我正在尝试在这里完成本教程——https://medium.freecodecamp.org/learn-how-to-create-your-first-angular-app-in-20-min-146201d9b5a7

我的 ./src/app/app.component.ts 文件顶部有这个

import { Component,trigger,animate,style,transition,keyframes } from '@angular/core';
import { FormsModule }   from '@angular/forms';

@NgModule({
  imports: [
             BrowserModule,
             FormsModule      //<----------make sure you have added this.
           ],
})

但是当我运行我的应用程序时,我得到了错误

localhost:todo-app davea$ ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2019-05-17T17:06:29.377Z                                                       
Hash: e9b5158cd1cb6d5685c7
Time: 2561ms
chunk {inline} inline.bundle.js (inline) 3.85 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 2.91 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 577 bytes [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 41.6 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 867 kB [initial] [rendered]

ERROR in src/app/app.component.ts(4,2): error TS2552: Cannot find name 'NgModule'. Did you mean 'module'?
src/app/app.component.ts(6,14): error TS2304: Cannot find name 'BrowserModule'.

我还需要做什么才能让 NgModule 被识别?

【问题讨论】:

  • 如果上面的代码是你最终得到的,那么你要么没有足够仔细地遵循教程,要么教程是垃圾。我认为是前者。
  • 你需要从@angular/core导入NgModule
  • @pjlamb12,将第一行更改为“import { Component,NgModule,trigger,animate,style,transition,keyframes } from '@angular/core';”确实有效。

标签: angular typescript npm node-modules


【解决方案1】:

这个错误Cannot find name 'NgModule' 是由于没有从@angular/core 导入NgModule。将其添加到第一行将解决问题。

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2018-07-25
    • 2016-07-12
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2019-07-09
    相关资源
    最近更新 更多