3
votes

Im trying to access the main route on my server at

http://localhost:4200

The full error message is:

Uncaught Error: Unexpected directive 'ProductsComponent' imported by the module 'AppModule'. Please add a @NgModule annotation. at syntaxError (compiler.es5.js:1690) at compiler.es5.js:15382 at Array.forEach () at CompileMetadataResolver.webpackJsonp.../../../compiler/@angular/compiler.es5.js.CompileMetadataResolver.getNgModuleMetadata (compiler.es5.js:15365) at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._loadModules (compiler.es5.js:26795) at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAndComponents (compiler.es5.js:26768) at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAsync (compiler.es5.js:26697) at PlatformRef.webpackJsonp.../../../core/@angular/core.es5.js.PlatformRef_.bootstrapModuleWithZone (core.es5.js:4536) at PlatformRef.webpackJsonp.../../../core/@angular/core.es5.js.PlatformRef_.bootstrapModule (core.es5.js:4522) at Object.../../../../../src/main.ts (main.ts:11)

Other router endpoints such as

http://localhost:4200/cart

also give the same error

Here is my main file

app.module.ts

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

    import { AppComponent } from './app.component';
    import { ProductsComponent } from './products/products.component';
    import { CartComponent } from './cart/cart.component';
    import { MenuComponent } from './menu/menu.component';
    import { AppRoutingModule, routingComponents } from './app.routes';

    @NgModule({
      declarations: [
        AppComponent,
        ProductsComponent,
        CartComponent,
        MenuComponent
      ],
      imports: [
        BrowserModule,
        RouterModule,
        AppRoutingModule,
        routingComponents
      ],
      providers: [],
      bootstrap: [AppComponent,routingComponents]
    })
    export class AppModule { }

Here is my rooting file

app.routes.ts

import { NgModule } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router';
import { ProductsComponent } from './products/products.component';
import { MenuComponent } from './menu/menu.component';
import { CartComponent } from './cart/cart.component';

const routes: Routes = [
   { path: '', pathMatch: 'full', redirectTo: 'Products' },
   { path: 'Products', component: ProductsComponent },
   { path: 'Menu', component: MenuComponent },
   { path: 'Cart', component: CartComponent },
 ];

 @NgModule({
   imports: [RouterModule.forRoot(routes)],
   exports: [RouterModule],
 })
 export class AppRoutingModule { }

 export const routingComponents = [ProductsComponent, MenuComponent, CartComponent];

The app seems to be starting fine on my local development.

2

2 Answers

7
votes

You can import only classes adorned by @NgModule decorator. So move routingComponent from imports to declarations:

 @NgModule({
    declarations: [
      AppComponent,
      ProductsComponent,
      CartComponent,
      MenuComponent,
      routingComponents // add here
    ],
    imports: [
      BrowserModule,
      RouterModule,
      AppRoutingModule
    ],
    ...
0
votes

Try removing the routingComponents. I don't see that that is doing anything and may be causing this problem.