基本的Javascript过滤函数在Angular2中不能正常工作

Basic Javascript filter function not working correctly in Angular2

本文关键字:常工作 不能 工作 Angular2 Javascript 过滤 函数      更新时间:2023-09-26

这应该非常简单,但是我在Typescript中的过滤器函数一直给我一个错误。下面是我的代码:

highScores: HighScore[];
deletePlayer(email: string) {
      this.scoreDataService.deletePlayer(email)
         .subscribe(
            this.highScores = this.highScores.filter(highScore => highScore.email !== email)
          );
}

过滤器函数应该简单地返回一个数组HighScore[],但相反,我一直得到这个错误:

Argument of type 'HighScore[] is not assignable to parameter of type 'NextObserver<Response> | ErrorObserver<Response> | CompletionObserver<Response> ...
Type 'HighScore[]' is not assignable to type '(value: Response) => void'. Tyope 'HighScore[]' provides no match for the signature '(value:Response): void'

最奇怪的是,即使有这个错误,这段代码也能完美地运行和工作。有人知道会发生什么吗?提前感谢!

subscribe期望一个函数参数:

this.scoreDataService.deletePlayer(email) 
  .subscribe(() => {
     this.highScores = this.highScores.filter(highScore => highScore.email !== email)
   });