【问题标题】:Basic Javascript filter function not working correctly in Angular2基本的 Javascript 过滤器功能在 Angular2 中无法正常工作
【发布时间】:2016-10-20 03:12:17
【问题描述】:

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

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

【问题讨论】:

  • 你的错误听起来很像这个:stackoverflow.com/questions/34738799/…
  • 我强烈怀疑代码能否完美运行。无论如何,您为什么认为过滤器功能无法正常工作,或者是什么导致了错误?仔细阅读错误消息 - 与大多数错误消息一样,它几乎可以准确地告诉您出了什么问题。

标签: javascript angular typescript


【解决方案1】:

subscribe 需要一个函数参数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 2015-07-17
    • 2021-07-04
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多