繁体   English   中英

错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数

[英]error TS2345: Argument of type 'Event' is not assignable to parameter of type 'IPokemon'

运行这段代码:

 <app-pokemon-form *ngIf="showPokemonForm" (newPokemonEvent)="addNewPokemon($event)" 
    (pokemonFormClose)="pokemonFormClose($event)"
      [pokemon]="currentPokemon"></app-pokemon-form>

导致错误:

错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数。

这是 TypeScript 文件:

 addNewPokemon(newPokemon: IPokemon): void {
    console.log('adding new pokemon ' + JSON.stringify(newPokemon));
    this.pokemonService.addPokemon({ ...newPokemon })
      .subscribe({
        next: pokemon => {
          console.log(JSON.stringify(pokemon) + ' has been added');
          this.message = "new pokemon has been added";
        },
        error: (err) => this.message = err
      });

这是口袋妖怪 model 本身:

export interface IPokemon {
    _id: string,
    Name: string,
    Generation: Number,
    Type: string
}

任何帮助将不胜感激

由于您的参数$eventEvent类型,这意味着它(不一定)具有以下形式

interface IPokemon {
  _id: string,
  Name: string,
  Generation: number,
  Type: string
}

您不能将$event传递给您的addPokemon方法。

您可以检查$event参数的形式是否与IPokemon接口匹配,如果

  • not :确保$event具有适当的形式,或者
  • 如果是:您可以在 function 通话中添加$event as unknown as IPokemon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM