角度AOT编译错误"无法确定类组件的模块



i有一个Angular(4.3.2)应用程序,我想在该应用程序上执行AOT构建。应用程序是使用@angular/cli创建的。我有两个用ng generate脚手架的组件和一个模块,其中两者都包含在声明中:

import {PrivateComponent} from './private.component/private.component';
NgModule({
   imports: [
      // other imports
      PrivateRoutingModule
   ],
   declarations: [
      ...some other components, 
      PrivateComponent, 
      AppTopBarComponent
   ]
   // other stuff 
}) 
export class PrivateModule {}

私有组件在模块的路由中也使用:

const routes: Routes = [
  {path: '', component: PrivateComponent, children: // other components}
] 
@NgModule({
   imports: [RouterModule.forChild(routes)] // this is the Angular built-in router module
})
export class PrivateRoutingModule {}

请注意如何在另一个模块中定义路由并导入PrivateModuleAppTopBarComponent用于PrivateComponent's模板内。因此两者都被使用和声明。但是,当我使用"node_modules/.bin/ngc" -p tsconfig-aot.json(我在Windows 10)时,我会收到此错误消息: Cannot determine the module for class PrivateComponent in (path-to-project)/src/app/pages/private/private.component/private.component.ts! Add PrivateComponent to the NgModule to fix it. Cannot determine the module for class AppTopBarComponent in (path-to-project)/src/app/pages/private/app.topbar.component.ts! Add AppTopBarComponent to the NgModule to fix it.。我的tsconfig-aot.json文件与Angular AOT构建指南中的文件完全相同。

确保您不要意外地有两个文件声明相同的组件

如果我运行ng g c时我在错误的目录中,这通常会发生在我身上,并且不会立即删除错误的生成文件。

错误在:无法确定类的模块 infyerboxGroupComponent In s:/.../src/app/common-widgets/features-widgets/features-box group/features-box group.component.ts! 将fairnerboxGroupComponent添加到ngmodule进行修复。

在此示例中,我在两个地方定义了FeatureBoxGroupComponent

srcappcommon-widgetsfeature-widgetsfeature-box-groupfeature-box-group.component.ts
srcappfeature-widgetsfeature-box-groupfeature-box-group.component.ts

当然,错误消息告诉我它的确切文件有问题 - 但是很容易浏览一下。

只需在文件中查找组件名称即可在任何地方查看它的引用,并确保仅定义一次。

我实际上找到了一个解决方案。问题是PrivateComponent是在另一个文件中导入但未使用的,仅此而已:

import { PrivateComponent } from '../private/private.component'; // not used anywhere

显然ngc试图链接所有内容,并被未使用的导入

混淆

这是我解决此问题的方式,假设您正在使用VScode

  1. 关闭所有打开选项卡。
  2. 停止ng serve
  3. 移动指示组件文件夹通过VScode
  4. 如果在移动后打开选项卡,请关闭它们并在关闭时保存更改(由于路径更改)。
  5. 现在应该使用--aot
  6. 运行ng build时进行编译

如果愿意,可以执行相同的过程将文件夹移回原始位置。

我解决问题后,检查了git diff并意识到问题是壳体。我将文件夹名称更改为上情况,但没有在路径中更新。

我有这个问题,当我使用ng build而不是ng build --prod时,它消失了。

显然我有两个我不使用但未从应用程序文件夹中删除的模块。它们也不在app.module.ts文件夹中声明。

根据文档,--prod标志也导致编译器也进行了一些死亡代码。

最新更新