错误NG6002:出现在AppModule的NgModule.imports中,但本身有错误

Hem*_*mar 12 javascript node.js typescript angular angular12

我在本地和建筑项目中有 Angular 12,它运行良好,没有任何错误。

我的本地设置:

Angular CLI: 12.0.5  
Node: 12.16.3  
Package Manager: npm 6.14.4  
OS: win32 x64  
Angular: 12.0.5
Run Code Online (Sandbox Code Playgroud)

但是,当在 Linux 服务器上构建我的项目时,它给出了这个错误,没有什么可处理的。

服务器设置:

Angular CLI: 12.0.5  
Node: 14.18.1  
Package Manager: npm 6.14.15  
OS: linux x64  
Angular: 12.0.5
Run Code Online (Sandbox Code Playgroud)
Error: src/app/app-routing.module.ts:34:14 - error NG6002: Appears in the NgModule.imports of AppModule, but itself has errors  
34 export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

应用程序模块.ts

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { NotifyFormComponent } from './notify-form/notify-form.component';
import { LoginComponent } from './login/login.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    HeaderComponent,
    FooterComponent,
    NotifyFormComponent,
    LoginComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

应用程序路由.module.ts

import { LoginComponent } from './login/login.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { NotifyFormComponent } from './notify-form/notify-form.component';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  {path:'',redirectTo:'/',pathMatch: 'full'},
  {
    path:'', 
    component: HomeComponent,
    children:[
      
    ]
  },
  {
    path:'notify', 
    component: NotifyFormComponent
  },
  {
    path:'login', 
    component: LoginComponent
  }
  // {
  //   path:'**', 
  //   component: HomeComponent
  // }
];

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

Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我在这里做错了什么,请帮助我。这只是一个简单的测试项目。预先非常感谢。

小智 33

我进行了一些研究狂热,这是一个官方问题https://github.com/angular/angular/issues/35399。另外,这里还有另一个问题,错误 NG6002: Appears in the NgModule.imports of AppModule, but Could not be returned to an NgModule class。但我会继续总结我发现的东西。

  • 对于许多人来说,只需重新启动并重建项目,或者执行完全强制的 npm 缓存清除并重新安装 node_modules 并重建它即可。

  • 人们建议在 tsconfig 中禁用“ivy”,但也有人提到,虽然它可以工作,但并不是推荐的修复方法。

  • 如果您将组件添加到导入而不是声明中,显然也会发生这种情况。

  • 如果您以 root 身份安装了某些软件包,或者由于某种原因您没有所有软件包的权限,显然也会发生这种情况。通过在目录上执行 chown 可以修复此问题,但真正的解决方案是首先正确安装软件包并配置 npm 配置,这样您就永远不会使用 root 权限安装软件包。因此,本质上,如果您使用 root 用户安装了软件包并尝试以非 root 身份运行该项目,则会出现此问题。

  • 有些人在将服务添加到导入而不是提供者时遇到错误。

  • 如果人们错误地命名包,即 SomeComponent 而不是 SomeComponentModule,也会出现此错误。

  • 在您的具体情况下,您在浏览器模块之前导入了应用程序路由器,我不知道这是否会导致问题,但我在任何地方都看到浏览器模块是在应用程序之前添加的,因此如果没有其他工作,请交换它们看看是否有效。

这就是我能找到的一切。