出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule 类

Posted

技术标签:

【中文标题】出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule 类【英文标题】:Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class 【发布时间】:2021-01-29 06:51:46 【问题描述】:

嗨, 我收到了上述问题。请帮助我解决如何解决此问题。 我已经按照这个error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class 进行了尝试。重新启动服务器并重新安装软件包(npm install),但错误仍然存​​在。

我的 navbar.component.ts 代码

import  Component, OnInit  from '@angular/core';

@Component(
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
)
export class NavbarComponent implements OnInit 

  constructor()  

  ngOnInit(): void 
  


我的 navbar.component.html 代码

<nav class="navbar navbar-expand-lg navbar-dark bg-danger">
  
    <a class="navbar-brand" routerLink="#"><B>hn</B></a>
    
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav">
        <a class="nav-item nav-link active" routerLink="HOME">HOME  <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link active" routerLink="ABOUT">ABOUT  <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link active" routerLink="CONTACT">CONTACT  <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link active" routerLink="ADDRESS">ADDRESS <span class="sr-only">(current)</span></a>
      </div>
    </div>
  </nav>

app.module.ts 代码

import  BrowserModule  from '@angular/platform-browser';
import  NgModule  from '@angular/core';
import  FormsModule  from '@angular/forms';
import  ChartsModule  from 'ng2-charts';
import  AppRoutingModule  from './app-routing.module';
import  AppComponent  from './app.component';
import  RoutingModule  from './routing/routing.module';
import  MyPageComponent  from './my-page/my-page.component';
import  GridModule  from '@syncfusion/ej2-angular-grids';
import  NavbarComponent  from './navbar/navbar.component';
import HttpClientModule from '@angular/common/http';
import  DataService  from './dataservice';


@NgModule(
  declarations: [
    AppComponent,
    MyPageComponent,
    NavbarComponent,

  ],

  imports: [
    BrowserModule,FormsModule,ChartsModule,
    AppRoutingModule,
    RoutingModule,GridModule,NavbarComponent,HttpClientModule
  ],
  
  providers: [DataService],
  bootstrap: [AppComponent]
)
export class AppModule  

mypage.component.ts 代码

import  Component, OnInit,ViewChild  from '@angular/core';
import  ChartDataSets, ChartOptions,ChartType  from 'chart.js';
import  Color, Label,SingleDataSet,monkeyPatchChartJsLegend,monkeyPatchChartJsTooltip  from 'ng2-charts';
import  DataService  from '../dataservice';
import  User  from '../User.module'; 
@Component(
  selector: 'app-my-page',
  templateUrl: './my-page.component.html',
  styleUrls: ['./my-page.component.css']
)
export class MyPageComponent implements OnInit 
public data: Object[];
  public lineChartData: ChartDataSets[] = [
     data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' ,
  ];
  public lineChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
  public lineChartOptions: any = 
    responsive: true,
  ;
  public lineChartColors: Color[] = [
    
      borderColor: 'black',
      backgroundColor: 'maroon',
    ,
  ];
  public lineChartLegend = true;
  public lineChartType = 'line';
  public lineChartPlugins = [];

【问题讨论】:

Module中不需要导入“NavbarComponent”。 【参考方案1】:

NavbarComponent 组件应该在declarations 中列出,而不是在imports 中。

【讨论】:

以上是关于出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule 类的主要内容,如果未能解决你的问题,请参考以下文章

错误NG6002:出现在AppModule的NgModule.imports中,但本身有错误

Angular 12迁移问题错误NG6002:出现在AppModule的NgModule.imports中,但本身有错误

模块“AppModule”导入的意外指令“LoginPage”。请添加@NgModule 注释

错误:找不到“AppModule”的 NgModule 元数据

未捕获的错误:模块“AppModule”导入的意外指令“MatFormField”。请添加@NgModule 注释

ERROR in AppModule is not an NgModule