【问题标题】:Angular tutorial Tour of Hero, what does "_" means in the following code [duplicate]Angular教程英雄之旅,以下代码中的“_”是什么意思[重复]
【发布时间】:2018-06-15 13:08:56
【问题描述】:

我正在关注英雄官方教程。

我在hero.service.ts收到一个问题,功能是使用HTTP PUT方法更新新英雄。

如下代码:

/** PUT: update the hero on the server */
updateHero(hero: Hero): Observable<any> {
  return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
     tap(_ => this.log(`updated hero id=${hero.id}`)),
     catchError(this.handleError<any>('updateHero'))
);

那么_在代码中的含义是什么?

【问题讨论】:

  • 它只是将单个参数命名为箭头函数_,大概是为了按照惯例表明他们实际上不会在函数中使用该参数。

标签: angular typescript


【解决方案1】:

_ 在这种情况下只是一个用于缩短箭头函数的空白标识符。所以在这种情况下

_ => this.log(`updated hero id=${hero.id}`)

相当于

() => this.log(`updated hero id=${hero.id}`)

_ 可以作为箭头函数中的参数访问的细微差别(尽管它可能具有值undefined),而第二个 sn-p 将没有任何可访问的参数。

最后,以_(或只是_ 本身)开头的变量在打字稿中具有特殊属性。在设置了--noUnusedParameters 标志的情况下,这些变量在不使用时不会导致编译错误。

【讨论】:

    猜你喜欢
    • 2017-03-26
    • 2017-07-18
    • 2017-04-22
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多