[英]“private” and “public” in Angular component
If I don't add private before foo
, loadBar
, and text
, I believe they are public by default. 如果我不在
foo
, loadBar
和text
之前添加private ,我相信它们默认是公共的。
export class RandomComponent {
@Input() foo: string;
@Output() loadBar = new EventEmitter();
text: string;
}
Is there any use case when they are public
in the component? 当它们在组件中
public
时是否有任何用例?
For encapsulation/security reason, should I always add private
for all of them like below? 出于封装/安全原因,我是否应该像以下一样为所有人添加
private
?
export class RandomComponent {
@Input() private foo: string;
@Output() private loadBar = new EventEmitter();
private text: string;
}
Thanks 谢谢
There's a lot to say in response to this question, these are the first thoughts that jumped to my mind: 回答这个问题有很多话要说,这些是我脑子里想到的第一个想法:
First and foremost, keep in mind that private
is only a compile-time construct - it cannot be enforced at runtime (see here and here for relevant discussion). 首先,请记住,
private
只是一个编译时构造 - 它不能在运行时强制执行(请参阅此处和此处进行相关讨论)。 As such, please disabuse yourself of any notions of private
being useful in any way for security purposes. 因此,为了安全起见,请以任何方式消除任何
private
用途的概念。 That's simply not what it's about. 这根本不是它的意思。
It is about encapsulation, and when you have a field or method on your component that you want to encapsulate in it, making it clear that it shouldn't be accessed from anywhere else, then you should absolutely make it private
: That's what private
is for: It signals your intent that whatever you've put it on shouldn't be touched from outside the class. 它是关于封装的,当你在组件上有一个字段或方法要封装在其中时,要明确它不应该从其他任何地方访问,那么你绝对应该把它变成
private
:这就是private
的for: 它标志着你的意图 ,无论你穿什么都不应该从课外接触。
The same goes for public
: It too is a compile-time-only construct, so the fact that class members are public
by default, while true, has exactly zero meaning at runtime. 对于
public
来说也是如此:它也是一个仅编译时的构造,因此默认情况下类成员是public
的,而true是在运行时具有完全零意义的事实。 But when you have a member that you explicitly intend to expose to the outside world as a part of your class's API, you should absolutely make it public
to signal this intent: That's what public
is for. 但是当你有一个明确打算作为你的类API的一部分暴露给外部世界的成员时,你绝对应该
public
表明这个意图:这就是public
的意思。
This is all applicable to Typescript in general. 这一般都适用于Typescript。 In Angular specifically, there are definitely valid use-cases for having public members on component classes: For instance, when implementing the container/component (aka smart/dumb ) pattern, with "dumb" children injecting "smart" parents via constructor injection, it's extremely important to communicate your intent about what members on the parent should and should not be touched by the children: Otherwise, don't be surprised when you catch those dumb kids fooling around in their parents' liquor cabinet.
特别是在Angular中,确实有公共成员使用组件类的有效用例:例如,在实现容器/组件 (又称智能/哑 )模式时,“哑”子项通过构造函数注入注入“智能”父项,非常重要的是要传达你对父母应该和不应该被孩子们触摸的意图的意图:否则,当你抓住那些在父母酒柜里鬼混的愚蠢孩子时,不要感到惊讶。
So, my answer to your question: 那么,我对你问题的回答:
should I always add private for all of them like below?
我应该像以下一样为所有人添加私人吗?
is an emphatic no . 是一个强调没有 。 You shouldn't always add
private
because in doing so you defeat the purpose of the keyword, because it no longer signals any intent if you put it everywhere: You might as well not put it anywhere. 你不应该总是添加
private
因为这样做会破坏关键字的目的,因为如果你把它放在任何地方它就不再发出任何意图:你可能不会把它放在任何地方。
@drewmoore provides a good answer in that private/public boils down to intent. @drewmoore提供了一个很好的答案,私人/公共归结为意图。 But there are a few more things to consider when using injected private values:
但是在使用注入的私有值时还有一些事情需要考虑:
@Import() private foo
, or constructor(private foo) {}
, and only use foo
in your template @Import() private foo
或constructor(private foo) {}
,则typescript( noUnusedLocals )和tslint( no-unused-variable )都会报告错误,并且只在模板中使用foo
If we want to emit TypeScript as output of the AoT compilation process we must make sure we access only public fields in the templates of our components**
如果我们想要将TypeScript作为AoT编译过程的输出发出,我们必须确保只访问组件模板中的公共字段**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.