[英]Private variable naming convention in official Angular 2 http tutorial
我試圖了解如何在Angular 2 http官方教程中命名私有變量
在上面鏈接的部分下面是一個名為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
。 好的,所以存在一個使用下划線啟動私有變量和方法的約定。
但那么為什么private http
和private handleError
也沒有使用下划線? 這只是一個“錯字”還是這個原因?
這只是一個錯字。 對於TS,這不是強制執行,它只是一個慣例。 在Angular2代碼庫中,它一直用於允許轉換為Dart,其中_
不僅是約定而是private
關鍵字的替代(Dart中不存在)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.