Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'id' of undefined at registerNgModuleType (core.js) after I added in appRoutes in my app

I added in appRoutes in my application and now when I compile my project, it just shows a blank page. I was able to inspect on Google Chrome and I am getting this error in the dialog box:

Uncaught TypeError: Cannot read property 'id' of undefined
    at registerNgModuleType (core.js:35926)
    at core.js:35944
    at Array.forEach (<anonymous>)
    at registerNgModuleType (core.js:35940)
    at new NgModuleFactory$1 (core.js:36102)
    at compileNgModuleFactory__POST_R3__ (core.js:41994)
    at PlatformRef.bootstrapModule (core.js:42357)
    at Module../src/main.ts (main.ts:12)
    at __webpack_require__ (bootstrap:79)
    at Object.0 (main.ts:13)

Here is my app.module.ts code:

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 { }

The IDE is also saying the following:

    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 {
                  ~~~~~~~~~~~~~~~~~

Is anyone familiar with this type of error? Or can you see anything in my code that might cause the issue?

like image 300
libracorn Avatar asked Apr 12 '20 07:04

libracorn


1 Answers

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

  providers: [ItemService],
  bootstrap: [AppComponent]
})

UPDATE your code with this snippet, you need to put your components inside declarations

like image 142
hectorromerodev Avatar answered Oct 22 '22 17:10

hectorromerodev