简体   繁体   English

Angular组件中的“私有”和“公共”

[英]“private” and “public” in Angular component

If I don't add private before foo , loadBar , and text , I believe they are public by default. 如果我不在fooloadBartext之前添加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: 但是在使用注入的私有值时还有一些事情需要考虑:

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.

相关问题 公共或私有 - Angular 2组件类方法混淆 - Public or private - Angular 2 component class methods confusion Angular4私有或公共方法? - Angular4 private or public method? 带有私人观察者的 Angular 2 多组件 - Angular 2 multiple component with a private observer 为什么 Angular 的依赖注入需要私有或公共才能工作? - Why does angular's dependency injection need private or public to work? 我需要从Angular 2中的其他组件获取公共方法 - I need get public method from other component in Angular 2 Typescript反映公共/私人 - Typescript reflect public / private Angular2错误:导出类的公共方法的返回类型具有或正在使用私有名称 - Angular2 Error: Return type of public method from exported class has or is using private name 感觉Angular中有第三个范围(除了公共和私有),但没有 - It feels like there's a third scope (besides public and private) in Angular but there isn't Angular2:来自导出类的公共静态方法的返回类型具有或正在使用私有名称 - Angular2: Return type of public static method from exported class has or is using private name 如何使用私有方法/属性来修改 output 的公共方法/属性的依赖项测试 Angular 6 服务 - How to test Angular 6 service with dependency that uses private methods/properties to modify output of public methods/properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM