【问题标题】:error NG6002: Appears in the NgModule.imports of AppModule, but itself has errors错误NG6002:出现在AppModule的NgModule.imports中,但本身有错误
【发布时间】:2021-12-15 23:49:15
【问题描述】:

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

我的本​​地设置:

Angular CLI: 12.0.5  
Node: 12.16.3  
Package Manager: npm 6.14.4  
OS: win32 x64  
Angular: 12.0.5

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

服务器设置:

Angular CLI: 12.0.5  
Node: 14.18.1  
Package Manager: npm 6.14.15  
OS: linux x64  
Angular: 12.0.5
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 { }

app.module.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 { }

app-routing.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 { }

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

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

【问题讨论】:

  • 尝试从路由中删除children:[],是否还有其他错误? ng build什么时候上线?

标签: javascript node.js angular typescript angular12


【解决方案1】:

您必须检查是否将所需的所有内容导入到 app.module.ts 中,然后检查 linux 是否需要添加任何其他组件,但这样应该可以正常工作

【讨论】:

    【解决方案2】:

    我进行了一些研究,这是https://github.com/angular/angular/issues/35399 的官方问题。这里还有另一个问题error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class。不过我还是会继续总结一下我发现的东西。

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

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

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

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

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

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

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

    这就是我能找到的一切。

    【讨论】:

    • 感谢@Matriarx 总结解决方案。我从最后尝试了您的列表,结果发现这是由于文件权限,因为我以 root 身份安装了软件包。
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2020-06-03
    • 2021-07-03
    • 2021-01-29
    • 2020-05-31
    • 1970-01-01
    • 2018-12-04
    • 2011-11-05
    相关资源
    最近更新 更多