【问题标题】:Property 'i' does not exist on type 'CustomGameComponent' after upgrade from Angular 8 to Angular 10从 Angular 8 升级到 Angular 10 后,“CustomGameComponent”类型上不存在属性“i”
【发布时间】:2020-11-11 19:54:26
【问题描述】:

当我使用 Angular 8 时,此代码编译时没有出现错误。我升级到 Angular 10,创建了一个新项目,当我复制并粘贴代码时出现此错误,并且输入上的标签显示“Player NaN”。 FormsModule 和 ReactiveFormModule 包含在我的 app.module.ts 中。

错误信息:


src/app/features/modules/games/custom-game/custom-game.component.html:4:34 - error TS2339: Property 'i' does not exist on type 'CustomGameComponent'.

4             <mat-label>Player {{ i + 1 }}</mat-label>
                                   ~

  src/app/features/modules/games/custom-game/custom-game.component.ts:6:16
    6   templateUrl: './custom-game.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component CustomGameComponent.
src/app/features/modules/games/custom-game/custom-game.component.html:5:73 - error TS2339: Property 'i' does not exist on type 'CustomGameComponent'.

5             <input matInput type="text" [formControl]="players.controls[i]">
                                                                          ~

  src/app/features/modules/games/custom-game/custom-game.component.ts:6:16
    6   templateUrl: './custom-game.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component CustomGameComponent.
src/app/features/modules/games/custom-game/custom-game.component.html:7:65 - error TS2339: Property 'i' does not exist on type 'CustomGameComponent'.

7         <button mat-mini-fab color="warn" (click)="removePlayer(i)">
                                                                  ~

  src/app/features/modules/games/custom-game/custom-game.component.ts:6:16
    6   templateUrl: './custom-game.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component CustomGameComponent.

HTML 文件:

<div class="body">
    <div ngFor="let control of players.controls; index as i">
        <mat-form-field>
            <mat-label>Player {{ i + 1 }}</mat-label>
            <input matInput type="text" [formControl]="players.controls[i]">
        </mat-form-field>
        <button mat-mini-fab color="warn" (click)="removePlayer(i)">
            <mat-icon>delete</mat-icon>
        </button>
    </div>
    <button mat-raised-button color="accent" (click)="addPlayer()">Add Player</button>
</div>

TS:

import { Component, OnInit } from '@angular/core';
import { FormArray, FormControl } from '@angular/forms';

@Component({
  selector: 'custom-game',
  templateUrl: './custom-game.component.html',
  styleUrls: ['./custom-game.component.scss']
})
export class CustomGameComponent implements OnInit {

  players = new FormArray([]);

  constructor() { }

  ngOnInit() {
  }

  addPlayer() {
    this.players.push(new FormControl(''));
  }

  removePlayer(index) {
    this.players.removeAt(index);
  }

}

【问题讨论】:

    标签: javascript angular typescript angular8 angular10


    【解决方案1】:

    我认为你必须使用 let i=index;

    【讨论】:

      【解决方案2】:

      这是

      *ngFor="let control of players.controls; index as i"
      

      不是

      ngFor="let control of players.controls; index as i"
      

      https://angular.io/api/common/NgForOf

      【讨论】:

      • 就是这样,谢谢您指出这一点。那个星号在旧项目中,我复制并粘贴了代码而没有更改任何内容,所以我不确定它是如何消失的。不管怎样,谢谢。
      猜你喜欢
      • 2021-11-21
      • 2018-10-16
      • 2019-11-07
      • 2019-12-24
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2020-10-31
      相关资源
      最近更新 更多