gpt4 book ai didi

angular - Angular 2 http官方教程中的私有(private)变量命名约定

转载 作者:太空狗 更新时间:2023-10-29 17:38:36 25 4
gpt4 key购买 nike

我试图了解官方 Angular 2 中私有(private)变量的命名方式 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');
}
}

有一个私有(private)变量_heroesUrl。好的,所以存在以下划线开头私有(private)变量和方法的约定。

但是为什么它不对 private httpprivate handleError 使用下划线呢?这只是一个“错字”还是一个原因?

最佳答案

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

关于angular - Angular 2 http官方教程中的私有(private)变量命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35256335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com