升级到 Angular v9 并启用常春藤后,Angular 编译失败



我们最近将angular应用程序升级到最新版本的Angular(Angular v9(。 我们所有的依赖项也都升级了,"ng update"表示我们所有的依赖项都是"有序的"。

当我们在启用 Ivy 的情况下构建应用程序时,编译过程失败并出现大量错误,这是我们以前从未遇到过的:

"angularCompilerOptions": {
"enableIvy": true
}

有些错误非常奇怪,说你不能绑定"ngClass"或"ngModel",因为它不是"div"的已知属性。它似乎缺少一些主要模块。

例如:

src/app/register/register.component.html:34:48 - error NG8002: Can't bind to 'ngClass' since it isn't a known property of 'div'.
<div class="form-group has-feedback" [ngClass]="{ 'has-error': f.submitted && !fname.valid }">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app/modals/modal-recommendations/modal-recommendations.component.html:12:25 - error NG8002: Can't bind to 'ngClass' since it isn't a known property of 'div'.
<div class="modal-body" [ngClass]="{'text-center': recommendationNotVisible()}">
12 <div class="modal-body" [ngClass]="{'text-center': recommendationNotVisible()}">
src/app/dashboard/dashboard.component.html:53:17 - error NG8002: Can't bind to 'accountId' since it isn't a known property of 'app-metric-box'.
53                 [accountId]="accountId"

或者它无法识别某些组件,例如:

src/app/export/export-base/export-base.component.html:2:5 - error NG8001: 'router-outlet' is not a known element:
1. If 'router-outlet' is an Angular component, then verify that it is part of this module.
2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
2     <router-outlet></router-outlet>

错误主要有两种类型:

  1. 不能绑定到 [some-property],因为它不是 [some-element] 的已知属性。这些属性可以是角度属性(ngClass,ngModel(或我们组件上的自定义属性。
  2. [某些组件] 不是已知元素(同样,我们的自定义组件和角度组件都会发生这种情况(

如果我们禁用"常春藤",一切正常,没有任何错误,代码编译并运行流畅。

我们想开始使用 Ivy,因此我们正在寻找有关这些错误的解释以及如何修复它们。

谢谢!

您需要将CUSTOM_ELEMENTS_SCHEMA添加到模块中的架构数组中

import { NgModule, CUSTOM_ELEMENTS_SCHEMA }  from '@angular/core';
@NgModule({
...
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule { }

我的问题是我有一个组件定义为入口组件。由于常春藤不推荐使用它,因此我从模块中删除了 entryComponents 属性。但是,在一种情况下,这会导致与您类似的错误。

error NG8002: Can't bind to 'ngClass' since it isn't a known property of 'div'

假设这发生在text.component.html(TextComponent(中。它有一个TextModule,其中包含CommonModule的导入,该模块启用ngClass。它位于ParentComponent下,以前在ParentModule中,它有一个TextComponent的入口属性。我怀疑Ivy只是在拉TextComponent而不是TextModule。所以我更新了 ParentModule 以导入文本模块,它解决了错误。

以前:

@NgModule({
imports: []
entryComponents: [TextComponent]
})
export class ParentModule { }

后:

@NgModule({
imports: [TextModule]
})
export class ParentModule { }

因此,您的个人问题可能有所不同,但我会看看您的模块和导入是如何构建的。

最新更新