【问题标题】:Property 'section 1' does not exist on type 'FormGroup'. aot build“FormGroup”类型上不存在属性“第 1 节”。自动构建
【发布时间】:2018-11-09 16:47:51
【问题描述】:

当我在 Angular 中进行 aot 构建时,出现错误“FormGroup”类型上不存在属性“第 1 节”。我已经添加了

<form [formGroup]="currentMonthForm" novalidate>
<table class="table spinwheel-table-blk">
      <thead>
        <tr COLSPAN=2 class="spinwheel-table-heading-block">
          <th>Section ID</th>
          <th><span>Points</span></th>
        </tr>
      </thead>
      <tbody>
        <tr COLSPAN=2>
          <td>1</td>
          <td>
            <div class="input-group wingsclub-inputgroup">
              <input type="text" class="form-control validation-field" placeholder="Points" aria-label="Recipient's username"
                aria-describedby="basic-addon2" maxlength="3" formControlName="section1" [ngClass]="{ 'is-invalid': currentMonthForm.section1 }"
                (input)="section1Change(1)">
              <small class="text-danger" *ngIf="sectionFormErrors.section1">
                <small class="text-danger" *ngIf="sectionFormErrors.section1">{{sectionFormErrors.section1}}</small>
              </small>
            </div>
          </td>
        </tr>
<table>

在组件中我有

currentMonthForm: FormGroup;
constructor(){
this.buildForm();
}
    buildForm() {
        this.currentMonthForm = this.fb.group({
          section1: [null, Validators.compose([Validators.required, Validators.maxLength(3),
          CustomValidators.number, this.validateNumber, CustomValidatorsInUse.isInteger, CustomValidatorsInUse.isPositiveInteger,])],})

但我收到错误“FormGroup”类型上不存在属性“第 1 节”。在 aot 构建中。但总的来说,它可以正常工作。

【问题讨论】:

  • 你能在这里展示整个组件吗?在正确指定之前,有些事情并不总是适用于 AoT 。 section1 属性是 protected 还是 private
  • sectionFormErrors的类型是什么?
  • sectionFormErrors 只是一个用于映射错误的对象
  • section1 只是来自控制值

标签: angular angular-aot


【解决方案1】:

看来罪魁祸首是这样的:

[ngClass]="{ 'is-invalid': currentMonthForm.section1 }"

JIT 编译器不会检查该属性是否真的存在于对象上,但 AOT 构建会检查并抱怨 currentMonthForm FormGroup 没有该属性。 您应该修复is-invalid 类的条件,因为它目前非常奇怪。我希望是这样的:

[ngClass]="{ 'is-invalid': currentMonthForm.get('section1').errors !== null }"

【讨论】:

  • 是的,这就是问题所在。
猜你喜欢
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 2021-03-05
  • 2018-10-04
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
相关资源
最近更新 更多