加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

官方Angular 2 http教程中的私有变量命名约定

发布时间:2020-12-17 07:14:29 所属栏目:安全 来源:网络整理
导读:我试图了解如何在官方Angular 2 http tutorial中命名私有变量 在上面链接的部分下面是一个名为app / toh / hero.service.ts的文件,其中(主要)是这样的: @Injectable()export class HeroService { constructor (private http: Http) {} private _heroesUrl =
我试图了解如何在官方Angular 2 http tutorial中命名私有变量

在上面链接的部分下面是一个名为app / toh / hero.service.ts的文件,其中(主要)是这样的:

@Injectable()
export class HeroService {
  constructor (private http: Http) {}

  private _heroesUrl = 'app/heroes';

  getHeroes () {
    return this.http.get(this._heroesUrl)
                    .map(res => <Hero[]> res.json().data)
                    .catch(this.handleError);
  }
  private handleError (error: Response) {
    // in a real world app,we may send the server to some remote logging infrastructure
    // instead of just logging it to the console
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}

有一个私有变量_heroesUrl.好的,所以存在一个使用下划线启动私有变量和方法的约定.

但那么为什么私有http和私有handleError也没有使用下划线?这只是一个“错字”还是这个原因?

解决方法

这只是一个错字.对于TS,这不是强制执行,它只是一个惯例.在Angular2代码库中,它一直用于允许转换为Dart,其中_不仅是约定而是private关键字的替代(Dart中不存在)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读