【问题标题】:Property does not exist on type Component during production build在生产构建期间,类型 Component 上不存在属性
【发布时间】:2019-06-27 15:37:23
【问题描述】:

我在我的组件中使用了一种反应形式,它在我的开发环境中运行良好。但是在生产构建时,我得到了 FormGroup 元素的错误

ClientApp\app\panel\applyLeave\applyLeave.component.html(86,36) 中的错误::“ApplyLeaveComponent”类型上不存在“reportingPerson”属性。 ClientApp\app\panel\applyLeave\applyLeave.component.html(99,36): :“ApplyLeaveComponent”类型上不存在属性“notifyPerson”。 ClientApp\app\panel\applyLeave\applyLeave.component.html(114,86): :“ApplyLeaveComponent”类型上不存在“正在加载”属性。 ClientApp\app\panel\applyLeave\applyLeave.component.html(86,36)::“ApplyLeaveComponent”类型上不存在“reportingPerson”属性。 ClientApp\app\panel\applyLeave\applyLeave.component.html(99,36): : 属性 'notifyPerson' 在类型 'ApplyLeaveComponent' 上不存在。

Component.ts 文件:-

export class ApplyLeaveComponent {
  leaveForm: FormGroup;

  ngOnInit() {
    this.leaveForm = this.fb.group({
      leaveType: ['', Validators.required],
      leaveFromDate: ['', Validators.required],
      leaveToDate: ['', Validators.required],
      leaveReason: ['', Validators.required],
      reportingPerson: ['', Validators.required],
      notifyPerson: ['', Validators.required]
    })
  }
}

component.html:-

    <form [formGroup]="leaveForm" class="row" 
    (ngSubmit)="applyLeave(leaveForm)">
      <div class="form-group col-2">
       <label for="" class="col-form-label">Leave type</label>
      <div class="">
      <select class="form-control form-control-sm" 
        formControlName="leaveType" required>
        <option value="">Select</option>
        <option *ngFor="let type of leaveType ;let i = index" 
        [value]="leaveType[i].id">{{type.type}}</option>
      </select>
     </div>
    <div *ngIf="submitted && f.leaveType.errors"> <div 
      *ngIf="f.leaveType.errors.required" style="color:red ; font- 
       size:13px">* <span>Required</span></div></div>
    </div>

   <div class="form-group col-3">
    <label for="" class="col-form-label">From Date</label>
    <div class="input-group add-on">
      <input class="form-control form-control-sm" placeholder="yyyy-mm-dd" 
      ngbDatepicker #d1="ngbDatepicker"
       [minDate]="FromMinDate" formControlName="leaveFromDate" required >

      <div class="input-group-btn col-4">
        <button class="btn fa fa-calendar " (click)="d1.toggle()" 
        type="button"></button>
      </div>
    </div>
    <div *ngIf="submitted && f.leaveFromDate.invalid"> <div 
    *ngIf="f.leaveFromDate.errors.required" style="color:red ; font- 
    size:13px">* <span>Required</span></div></div>
    </div>

  <div class="form-group col-3">
    <label for="" class="col-form-label">To Date</label>
    <div class="input-group add-on">
      <input class="form-control form-control-sm" placeholder="yyyy-mm-dd" 
      ngbDatepicker #d2="ngbDatepicker"
      [minDate]="ToMinDate" formControlName="leaveToDate" required>
      <div class="input-group-btn col-4">
        <button class="btn fa fa-calendar " (click)="d2.toggle()" 
         type="button"></button>
      </div>
    </div>
    <div *ngIf="submitted && f.leaveToDate.invalid"> <div 
     *ngIf="f.leaveToDate.errors.required" style="color:red ; font- 
      size:13px">* <span>Required</span></div></div>
    </div>

  <div class="form-group col-4">
    <label for="" class="col-form-label">Reporting person</label>
    <div class="">
      <ng-multiselect-dropdown [placeholder]="'Select'"
                               [data]="reportingPersonList"
                               [settings]="reportingdropdownSettings"
                               [(ngModel)]="reportingPerson"
                               formControlName="reportingPerson"
                               required>
      </ng-multiselect-dropdown>
    </div>
    <div *ngIf="submitted && f.reportingPerson.invalid"> <div 
     *ngIf="f.reportingPerson.errors.required" style="color:red ; font- 
      size:13px">* <span>Required</span></div></div>
   </div>
  <div class="form-group col-4">
    <label for="" class="col-form-label">Notify</label>
    <div class="">
      <ng-multiselect-dropdown [placeholder]="'Select'"
                               [data]="notifyList"
                               [settings]="dropdownSettings"
                               [(ngModel)]="notifyPerson"
                               formControlName="notifyPerson"
                               required>
      </ng-multiselect-dropdown>
    </div>
    <div *ngIf="submitted && f.notifyPerson.invalid"> <div 
      *ngIf="f.notifyPerson.errors.required" style="color:red ; font- 
       size:13px">* <span>Required</span></div></div>
   </div>
  <div class="form-group col-5">
    <label for="" class="col-form-label">Reason</label>
    <div class="">
      <textarea class="form-control form-control-sm" 
      formControlName="leaveReason" required></textarea>
    </div>
    <div *ngIf="submitted && f.leaveReason.invalid"> <div 
      *ngIf="f.leaveReason.errors.required" style="color:red ; font- 
      size:13px">* <span>Required</span></div></div>
    </div>
  <div class="col-3">
    <button type="submit" class="btn btn-primary" name="submit" 
    id="leavebutton" [disabled]="loading">Apply</button>
  </div>
</form>

我尝试将我的表单组的类型用作leaveForm:any=FormGroup;,但这并没有解决问题

【问题讨论】:

  • 请显示applyLeave.component.html
  • 问题已更新

标签: angular angular7


【解决方案1】:

由于您已绑定属性notifyPerson 和其他提到的属性,如[(ngModel)]="notifyPerson",您需要在您的 ts 中声明它。

喜欢:

notifyPerson:string
reportingPerson:string

您还需要像这样声明属性loading

loading:boolean

注意:避免在 Reactive 形式中使用 [(ngModel)]

【讨论】:

  • 另一种方式是什么?如果我没有绑定,我该如何进行?任何文档链接就足够了
  • 这是推荐的方式。生产构建检查一切。你应该在使用它之前声明一个属性
  • @AdritaSharma,选择选项(下拉)呢?
猜你喜欢
  • 2021-02-08
  • 2021-03-05
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 2021-01-13
  • 2017-12-02
相关资源
最近更新 更多