libracorn
libracorn

Reputation: 317

Angular application not building NgModule

I have an Angular application, but I am not understanding what the compiler is telling me on why the application can't build. These are the errors that I'm getting from the compiler:

ERROR in src/app/list-items/list-items.component.ts:9:14 -
error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class.

Is it missing an @NgModule annotation?

9 export class ListItemsComponent implements OnInit {
               ~~~~~~~~~~~~~~~~~~
src/app/not-found/not-found.component.ts:8:14 - error NG6002: Appears in the NgModule.imports of AppModule, but could
not be resolved to an NgModule class.

Is it missing an @NgModule annotation?

8 export class NotFoundComponent implements OnInit {
               ~~~~~~~~~~~~~~~~~

This is my list-items.component.ts file:

import { Component, OnInit, NgModule } from '@angular/core';

@Component({
  selector: 'app-list-items',
  templateUrl: './list-items.component.html',
  styleUrls: ['./list-items.component.css']
})
export class ListItemsComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }
}

This is my not-found.component.ts file:

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

@Component({
  selector: 'app-not-found',
  templateUrl: './not-found.component.html',
  styleUrls: ['./not-found.component.css']
})
export class NotFoundComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }
}

Here is my app.module.ts file:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';  
import { ItemService } from './items.service';
import { NewItemFormComponent } from './new-item-form/new-item-form.component';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatMenuModule } from '@angular/material/menu';
import { MatIconModule } from '@angular/material/icon';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';
import { NavigationMenuComponent } from './navigation-menu/navigation-menu.component';
import { ListItemsComponent } from './list-items/list-items.component';
import { NotFoundComponent } from './not-found/not-found.component';
import { BrowserModule } from '@angular/platform-browser';

const appRoutes: Routes = [ {
  path: '',                     //default component to display
   component: ListItemsComponent
 },       {
   path: 'addItem',         //when items added 
   component: NewItemFormComponent
 },       {
   path: 'listItems',       //when items listed
   component: ListItemsComponent
 },       {
   path: '**',                 //when path cannot be found
   component: NotFoundComponent
 }
];

@NgModule({
  declarations: [
    AppComponent,
    NewItemFormComponent,
    NavigationMenuComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatIconModule,
    MatMenuModule,
    BrowserAnimationsModule,
    FormsModule,
    RouterModule.forRoot(appRoutes),
    ListItemsComponent,
    NotFoundComponent
  ],

  providers: [ItemService],
  bootstrap: [AppComponent]
})
export class AppModule { }

Does anyone happen to understand what the compiler is trying to tell me about NgModule?

Upvotes: 1

Views: 6219

Answers (1)

kacase
kacase

Reputation: 2859

Remove both components from the imports section in your app.modules file and move them to declarations.

You are not creating a custom module; you are only creating a component. Import it the correct way and you should not have any more problems.

I also suggest you use the angular cli generator to generate new components.

ng generate component test

Upvotes: 4

Related Questions