Angular 角度AOT编译错误“;无法确定类组件的模块'';
我有一个Angular(4.3.2)应用程序,我想在其上执行AOT构建。应用程序是使用Angular 角度AOT编译错误“;无法确定类组件的模块'';,angular,typescript,angular-cli,aot,angular-aot,Angular,Typescript,Angular Cli,Aot,Angular Aot,我有一个Angular(4.3.2)应用程序,我想在其上执行AOT构建。应用程序是使用@angular/cli创建的。我用ng generate构建了两个组件和一个模块,这两个组件都作为声明包含在其中: import {PrivateComponent} from './private.component/private.component'; NgModule({ imports: [ // other imports PrivateRoutingModule
@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 {}
注意路由是如何在另一个模块中定义并导入到PrivateModule
AppTopBarComponent
在PrivateComponent的
模板中使用。因此,两者都被使用和声明。但是,当我使用“node_modules/.bin/ngc”-p tsconfig aot.json
(我在Windows 10上)时,会收到以下错误消息:
无法在(项目路径)/src/app/pages/private/private.component/private.component.ts中确定类PrivateComponent的模块!将PrivateComponent添加到NgModule以修复它。
无法确定(项目路径)/src/app/pages/private/app.topbar.component.ts中AppTopBarComponent类的模块!将AppTopBarComponent添加到NgModule以修复它。
。我的tsconfig aot.json
文件与中的完全相同。我实际上找到了一个解决方案。问题是,PrivateComponent
已导入另一个文件,但未使用,如下所示:
import { PrivateComponent } from '../private/private.component'; // not used anywhere
显然,ngc
试图链接所有内容,但被未使用的导入弄糊涂了请确保不要意外地有两个文件声明了相同的组件
如果我在运行nggc
时位于错误的目录中,并且没有立即删除错误生成的文件,这种情况经常发生
错误:无法确定类的模块
中的FeatureBoxGroupComponent
S:/…/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
当然,错误消息会告诉我它遇到问题的确切文件,但是很容易浏览一下
只需在文件中查找组件名称,就可以看到它引用的所有地方,并确保它只定义了一次。我遇到了这个问题,当我使用
ng build
而不是ng build--prod
时,它消失了
显然,我有两个模块,我没有使用,但没有从应用程序文件夹中删除。它们也没有在app.module.ts文件夹中声明
根据文档,
--prod
标志也会导致编译器执行一些死代码消除。假设您使用的是VScode
,下面是我解决此问题的方法
ng发球
VScode
将指定的组件文件夹移动到其他位置--aot
另外,在我修复问题后,我检查了
git diff
,发现问题出在外壳上。我将文件夹名称改为大写,但路径中没有更新。值得一提的是,我遇到了几个问题,但问题主要是未使用的组件。实际上使用了我的。您忘记在从“./private.component/private.component”导入{PrivateComponent}时添加PrivateComponent@维加我的错,我只是错过了它,而复制粘贴。它实际上存在于源代码中。第一个@for NgModule也是?:)还要确保导入路径的大小写与实际的文件路径大小写匹配。在我的情况下,我必须复制并拖动文件夹。谢谢这帮我弄明白了!我在另一个项目中将新的构建文件手动复制到我的节点_模块
,以进行测试,显然只需要先删除所有旧的构建文件。Angular 9应该已经改进了此错误消息:-)