基本Javascript过滤器功能在Angular2中无法正常工作

时间:2016-10-19 19:08:07

标签: javascript angular typescript

这应该非常简单,但我在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'

最奇怪的是,即使出现此错误,此代码也能正常运行。有谁知道会发生什么?提前谢谢!

1 个答案:

答案 0 :(得分:2)

subscribe需要一个函数参数:

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