Search code examples
androidangularionic-frameworkionic4

Uncaught Error: Unexpected directive 'NavComponent' imported by the module 'AppModule'. Please add a @NgModule annotation


I've a custom component (nav) inside.

When I use it , webpack compilation ends successfully but chrome throws the following error:

Uncaught Error: Unexpected directive 'NavComponent' imported by the module 'AppModule'. Please add a @NgModule annotation.

Here is my AppModule:

>

 import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { NavComponent } from './nav/nav.component';

@NgModule({
  declarations: [AppComponent ],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, NavComponent],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Solution

  • If your NavComponent is a @Component or a @Directive, you need to add it into the declarations, instead of imports

    ...
    declarations: [ AppComponent, NavComponent ],
    ...