[英]Private variable naming convention in official Angular 2 http tutorial
I am trying to understand how private variable is named in the official Angular 2 http tutorial 我试图了解如何在Angular 2 http官方教程中命名私有变量
Under the section linked above is a file called app/toh/hero.service.ts
, which (mainly) is this: 在上面链接的部分下面是一个名为
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');
}
}
There is a private variable _heroesUrl
. 有一个私有变量
_heroesUrl
。 Ok, so there exists a convention to start private variables and methods with underscore. 好的,所以存在一个使用下划线启动私有变量和方法的约定。
But then why is it not used an underscore also for private http
and private handleError
? 但那么为什么
private http
和private handleError
也没有使用下划线? Is it just a "typo" or is it a reason for this? 这只是一个“错字”还是这个原因?
This is just a typo. 这只是一个错字。 For TS this is not enforced, it's just a convention.
对于TS,这不是强制执行,它只是一个惯例。 Within the Angular2 codebase this is used consistently to allow transpilation to Dart where the
_
is not only a convention but a replacement for the private
keyword (which doesn't exist in Dart) 在Angular2代码库中,它一直用于允许转换为Dart,其中
_
不仅是约定而是private
关键字的替代(Dart中不存在)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.