简体   繁体   English

在 angular 2 应用程序中使用 getter setter 是一个好习惯吗

[英]Is it a good practice to use getter setter in angular 2 application

I am working on angular2 application in which my team member are using getter and setter to set input properties as follow我正在开发 angular2 应用程序,其中我的团队成员使用 getter 和 setter 来设置输入属性,如下所示

private _showModal;
@Input() set showModal(showModal){
     this._showModal = showModal;
}
get showModal() {
     return this._showModal;
}

but I am not sure its a good way to do this.但我不确定这是一个很好的方法来做到这一点。 I thought one should use getter setter in the case where dev have to do some validation or check or do some other function while setting or getting value我认为在开发人员必须在设置或获取值时进行一些验证或检查或执行其他一些功能的情况下,应该使用 getter setter

Where we do the reading( get ) and writing( get ) majorly determines how much cost we pay for performance.我们在哪里进行读取( get )和写入( get )主要决定了我们为性能支付的成本。

set ters set

A set ter function is called every time we write some value.set之三函数被调用每次我们写一些价值的时间。

Now we generally do the writing part that calls the set ters in our TypeScript Classes.现在我们通常会在我们的 TypeScript 类中调用set ter 的编写部分。 So they don't get called so often unless there's a set operation, which isn't quite frequent in general.因此,除非有set操作,否则它们不会经常被调用,这通常不是很频繁。

get ters get ter

A get ter function is called every time we read some value.一个get之三函数被调用每当我们看到一些价值的时间。

get ters are generally called in templates in different data binding syntax like string interpolation( {{}} ), property binding( [---]="" ), attribute binding( [attr.---]="" ), style binding( [style.---]="" ) etc. get ter 通常在模板中以不同的数据绑定语法调用,如字符串插值( {{}} )、属性绑定( [---]="" )、属性绑定( [attr.---]="" )、样式绑定( [style.---]="" ) 等。

Now the catch with this is, every time Angular performs change detection, the get ters get called.现在的问题是,每次 Angular 执行更改检测时,都会调用get ter。 It's fine as long as there isn't much logic in your get ter.只要你的get ter 没有太多逻辑就get But that still leaves a room for newer developers in the team to add logic in there without being aware of the performance hits that it's gonna create.但这仍然为团队中的新开发人员留出了空间,可以在其中添加逻辑,而无需意识到它会产生的性能影响。

So in summary, from what I understand, it's okay to have set ters.所以总而言之,根据我的理解, set ter 是可以的。 But having get ters and it's cost on performance would mainly depend on where those get ters are used.但是拥有get ter 以及它的性能成本主要取决于这些get ter 的使用位置。 If they're used in one of the template binding syntaxes, then it's safe to NOT HAVE THEM IN THE FIRST PLACE.如果它们在模板绑定语法之一中使用,那么首先不要使用它们是安全的。 If they're not being used in the template, it's okay to have them.如果模板中未使用它们,则可以使用它们。

I've actually written an article and a few answers on various StackOverflow Threads that you might want to check out as well.我实际上写了一篇关于各种 StackOverflow 线程的文章和一些答案,您可能也想查看。 So I'm adding them as a list below:所以我将它们添加为下面的列表:

  1. Angular: Prevent DomSanizer from updating on DOM Events Angular:防止 DomSanizer 更新 DOM 事件

  2. Angular performance: ngStyle recalculates on each click on random input Angular 性能:ngStyle 在每次点击随机输入时重新计算

  3. Angular 7 ,Reactive Form slow response when has large data Angular 7 ,Reactive Form 有大数据时响应慢

  4. Angular Performance: DOM Event causes unnecessary function calls Angular 性能:DOM 事件导致不必要的函数调用

  5. I changed my implementation of an EXTREMELY deeply nested Angular Reactive Form and you won't believe what happened 🤯 我改变了我对一个极其深层嵌套的 Angular Reactive Form 的实现,你不会相信发生了什么🤯

Hope this gives you some perspective.希望这能给你一些视角。 :) :)

This is part opinion, part the requirements of your application.这是部分意见,部分是您的应用程序要求。 It is certainly not bad to use getters and setters.使用 getter 和 setter 当然也不错 But I would also use them with discretion and in most cases getters and setters may be unnecessary.但我也会谨慎使用它们,在大多数情况下,getter 和 setter 可能是不必要的。

In the example code you provide, there is no reason to use a getter and setter.在您提供的示例代码中,没有理由使用 getter 和 setter。 You are correct in that it can help when doing some sort of validation check or when something else in contingent upon the value being set, etc. For example, maybe you need to call some function when an @Input() property changes value, this can be an easy way to accomplish that.您是正确的,因为它可以在进行某种验证检查或根据设置的值等情况进行其他操作时有所帮助。例如,当@Input()属性更改值时,您可能需要调用某个函数,这可以是实现这一目标的简单方法。 But in many cases, this is probably not a requirement for every variable/input in your application.但在许多情况下,这可能不是应用程序中每个变量/输入的要求。

Hope this helps.希望这可以帮助。

avoid getters specifically if possible.如果可能,请特别避免使用吸气剂。 Getters have a negative effect on change detection. Getter 对变更检测有负面影响。 In order to know whether the view should be updated, Angular needs to access the new value, compare it with the old one and make the decision on whether the view should be updated.为了知道视图是否应该更新,Angular 需要访问新值,将它与旧值进行比较,然后决定是否应该更新视图。 For this reason, the value is compared and updated on every change detection cycle.因此,在每个变更检测周期都会比较和更新该值。 This can cause performance issues and circumvents certain configurations like OnPush.这可能会导致性能问题并绕过某些配置,例如 OnPush。 If for some reason you need to transform data when an @Input() is changed in a component a setter is fine for example.例如,如果由于某种原因需要在组件中更改 @Input() 时转换数据,则 setter 就可以了。

value;
@Input('myValue') set myValue(v) {
 transformValue(v);
}
transformValue(v) {
...*sometransform*
this.value = transforrmedValue
}

in this example, a setter is placed on the Input and the value is transformed every time a new myValue is pushed to the component.在这个例子中,一个 setter 被放置在 Input 上,并且每次将新的 myValue 推送到组件时都会转换值。 However, if a getter is introduced the component will check the getter every time change detection cycles anyway.但是,如果引入了 getter,则组件将在每次更改检测循环时检查 getter。 You could also use other things like pipes or ngOnChange instead of setters.您还可以使用其他东西,如管道或 ngOnChange 而不是 setter。

UPDATE更新

as long as your component is using ChangedetectionStategy.OnPush now using a getter is fine but still avoid it if you are not using OnPush只要您的组件正在使用 ChangedetectionStategy.OnPush 现在使用 getter 就可以了,但如果您不使用 OnPush 仍然避免它

I would only do this if you alter _showModal in the get or set.如果您在 get 或 set 中更改 _showModal,我只会这样做。

Using it like your team member does (with a backing property) just adds more lines of code.. In that case I would make a public showModal property.像您的团队成员一样使用它(带有支持属性)只会添加更多代码行。在这种情况下,我会创建一个公共 showModal 属性。

I don't know if there is a "best practice" for this.我不知道这是否有“最佳实践”。 I think it mostly has to do with personal preference.我认为这主要与个人喜好有关。

It is a good practice as you have more control on your data.这是一个很好的做法,因为您可以更好地控制自己的数据。 Especially when you need to know when your data has changed with angular change detection.尤其是当您需要通过角度变化检测了解数据何时发生变化时。

Most of the time, I use them in my services to share the data over several components.大多数时候,我在我的服务中使用它们来共享多个组件的数据。

They also interact well with observables to avoid calling several times an endpoint to get your data.它们还与 observable 进行良好的交互,以避免多次调用端点来获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM