在Angular2 +中正确命名属性 [英] Correct naming of properties in Angular2+

查看:36
本文介绍了在Angular2 +中正确命名属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要写这个:

private _name:string;
public get name():string{
    return this._name;
}

但是《 Angular样式指南》建议不要这样做: https://angular.io/styleguide#!#03-04

But the Angular Style Guide advise against it: https://angular.io/styleguide#!#03-04

我想错了吗?我应该怎么用呢?

Am I thinking wrong? What should I use instead?

推荐答案

您使用的是TypeScript标准.由于您无法命名变量名,因为 get name()将被编译为 whatever.name ,因此必须使用下划线.

What you're using is TypeScript standard. Since you can't name your variable name, because get name() will be compiled to whatever.name, you have to use the underscore.

Angular在那里没有什么不同,因为它使用TypeScript本身.他们只是说如果您没有针对该属性的特定getter或setter方法,则应使用name.

Angular isn't any different there because it uses TypeScript itself. They just say you should use name if you don't have a specific getter or setter method for that property.

我不知道您的代码是否仅是示例,但由于属性名称最终只是公开地公开,因此没有任何意义.

I don't know if your code is just an example but it wouldn't make sense since the property name simply would be public in the end anyway.

这篇关于在Angular2 +中正确命名属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆