未捕获错误:意外指令' ProductsComponent'由模块' AppModule'导入。请添加@NgModule注释

时间:2017-08-22 20:02:49

标签: javascript angular

我试图访问我服务器上的主路径

  

http://localhost:4200

完整的错误消息是:

  

未捕获错误:意外指令' ProductsComponent'进口   模块' AppModule'。请添加@NgModule注释。       在syntaxError(compiler.es5.js:1690)       在compiler.es5.js:15382       在Array.forEach()       at CompileMetadataResolver.webpackJsonp ... / .. / .. / compiler/@angular/compiler.es5.js.CompileMetadataResolver.getNgModuleMetadata   (compiler.es5.js:15365)       在JitCompiler.webpackJsonp ... / .. / .. / compiler/@angular/compiler.es5.js.JitCompiler._loadModules   (compiler.es5.js:26795)       在JitCompiler.webpackJsonp ... / .. / ../ compiler /@angular/compiler.es5.js.JitCompiler。 compileModuleAndComponents   (compiler.es5.js:26768)       在JitCompiler.webpackJsonp ... / .. / ../ compiler /@angular/compiler.es5.js.JitCompiler.compileModuleAsync   (compiler.es5.js:26697)       在PlatformRef .webpackJsonp ... / .. / .. / core/@angular/core.es5.js.PlatformRef _。 bootstrapModuleWithZone   (core.es5.js:4536)       在PlatformRef .webpackJsonp ... / .. / .. / core/@angular/core.es5.js.PlatformRef_.bootstrapModule   (core.es5.js:4522)       在对象... / .. / .. / .. / .. / src / main.ts(main.ts:11)

其他路由器端点,例如

  

http://localhost:4200/cart

也会出现同样的错误

这是我的主要文件

  

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

这是我的生根文件

  

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];

该应用程序似乎在我的本地开发中正常运行。

2 个答案:

答案 0 :(得分:6)

你可以import only classes adorned by @NgModule decorator。所以将routingComponentimports移到declarations

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

答案 1 :(得分:0)

尝试删除routingComponents。我不知道那是在做什么,可能会导致这个问题。