简体   繁体   English

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

[英]Angular AOT compilation error "cannot determine module for class Component''

I have an Angular (4.3.2) application on which I want to perform an AOT build. 我有一个Angular(4.3.2)应用程序,我想在其上执行AOT构建。 App was created using @angular/cli . 应用程序是使用@angular/cli创建的。 I have two components scaffolded with ng generate and a module in which both are included as a declaration: 我有两个使用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 {}

Private component is also used in the module's routing: 私有组件也用于模块的路由:

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

Notice how the routing was defined in another module and imported into PrivateModule The AppTopBarComponent is used inside the PrivateComponent's template. 注意如何在另一个模块中定义路由并将其导入PrivateModule AppTopBarComponentPrivateComponent's模板中使用。 So both are used and declared. 所以两者都被使用和宣布。 But when I use "node_modules/.bin/ngc" -p tsconfig-aot.json (I am on Windows 10), I get this error message: 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. 但是,当我使用"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 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. . My tsconfig-aot.json file is exactly the same as is in the Angular AOT build guide . 我的tsconfig-aot.json文件与Angular AOT构建指南中的文件完全相同。

Make sure you don't accidentally have two files declaring the same component 确保您不会意外地有两个文件声明相同的组件

This often happens to me if I'm in the wrong directory when I run ng gc , and don't immediately delete the wrong generated file. 如果我在运行ng gc时遇到错误的目录,并且不立即删除错误生成的文件,这通常会发生在我身上。

ERROR in : Cannot determine the module for class FeatureBoxGroupComponent in S:/.../src/app/common-widgets/feature-widgets/feature-box-group/feature-box-group.component.ts! 错误输入:无法确定S中的类FeatureBoxGroupComponent的模块:/ ... / src / app / common-widgets / feature-widgets / feature-box-group / feature-box-group.component.ts! Add FeatureBoxGroupComponent to the NgModule to fix it. 将FeatureBoxGroupComponent添加到NgModule以修复它。

In this example I had FeatureBoxGroupComponent defined in two places: 在这个例子中,我在两个地方定义了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

Of course the error message is telling me the exact file it has a problem with - but it's easy to glance over that. 当然,错误消息告诉我它有问题的确切文件 - 但是很容易看一眼。

Just do a find in files for the component name to see everywhere it's referenced and make sure it's only defined once. 只需在文件中查找组件名称,即可查看引用的所有位置,并确保仅定义一次。

I have actually found a solution. 我实际上找到了一个解决方案。 The problem was that PrivateComponent was imported in another file, but not used, just like this: 问题是PrivateComponent是在另一个文件中导入的,但没有使用,就像这样:

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

Apparently ngc tries to link everything and is confused by an unused import 显然, ngc尝试链接所有内容,并被未使用的导入混淆

I had this issue and it disappeared when I used ng build instead of ng build --prod . 我有这个问题,当我使用ng build而不是ng build --prod时它就消失了。

Apparently I had two modules that I was not using but had not deleted from the app folder. 显然我有两个模块,我没有使用,但没有从应用程序文件夹中删除。 They were not declared in the app.module.ts folder either. 它们也没有在app.module.ts文件夹中声明。

As per the documentation the --prod flag causes the compiler to carry out some dead code elimination as well. 根据文档,-- --prod标志--prod导致编译器执行一些死代码消除。

Here is how I solved this problem, assuming you are using VScode . 这是我如何解决这个问题,假设您正在使用VScode

  1. Close all the open tabs. 关闭所有打开的标签页。
  2. Stop ng serve 停止ng serve
  3. Move indicated component folders to some other location through VScode . 通过VScode将指示的组件文件夹移动到其他位置。
  4. If tabs open after moving, close them and save the changes while closing (Due to path change). 如果在移动后打开选项卡,请关闭它们并在关闭时保存更改(由于路径更改)。
  5. It should now compile when you run ng build with --aot 现在应该在使用--aot运行ng build进行--aot

If you like, you can do the same process to move folders back to original locations. 如果您愿意,可以执行相同的过程将文件夹移回原始位置。

Also, after I fixed the problem checked the git diff and realized that the problem was the casing. 另外,在我修复问题后检查git diff并意识到问题是外壳。 I changed the folder names to upper case but that did not get updated in the path. 我将文件夹名称更改为大写,但未在路径中更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM