gpt4 book ai didi

javascript - 未捕获错误 : Unexpected directive 'ProductsComponent' imported by the module 'AppModule' . 请添加 @NgModule 注解

转载 作者:行者123 更新时间:2023-11-29 16:05:00 25 4
gpt4 key购买 nike

我正在尝试访问我服务器上的主路由

http://localhost:4200

完整的错误信息是:

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)

其他路由器端点如

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

这是我的root文件

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

该应用程序似乎在我的本地开发中启动良好。

最佳答案

您可以 import only classes adorned by @NgModule decorator .所以将 routingComponentimports 移动到 declarations:

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

关于javascript - 未捕获错误 : Unexpected directive 'ProductsComponent' imported by the module 'AppModule' . 请添加 @NgModule 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45826083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com