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

Arm*_*yan 17 aot typescript angular-cli angular angular-aot

我有一个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 {}
Run Code Online (Sandbox Code Playgroud)

私有组件也用于模块的路由:

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 {}
Run Code Online (Sandbox Code Playgroud)

注意:路由是如何在另一个模块定义和导入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构建指南中的文件完全相同.

Sim*_*ver 27

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

如果您有两个组件定义,也会出现类似的问题.如果我在运行时位于错误的目录中ng g c,或者我只是将错误的路径放入其中并且不立即删除错误的文件,则会发生这种情况.

错误输入:无法确定S中的类FeatureBoxGroupComponent的模块:/ .../src/app/common-widgets/feature-widgets/feature-box-group/feature-box-group.component.ts!将FeatureBoxGroupComponent添加到NgModule以修复它.

FeatureBoxGroupComponent在两个地方定义了:

src\app\common-widgets\feature-widgets\feature-box-group\feature-box-group.component.ts

src\app\feature-widgets\feature-box-group\feature-box-group.component.ts
Run Code Online (Sandbox Code Playgroud)

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


Arm*_*yan 17

我实际上找到了一个解决方案.问题是PrivateComponent在另一个文件中导入但未使用,如下所示:

import { PrivateComponent } from '../private/private.component'; // not used anywhere
Run Code Online (Sandbox Code Playgroud)

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

  • 还要确保导入路径大小写与实际文件路径大小写匹配. (2认同)