简体   繁体   English

Angular Reactive Forms vs ngModel,哪个性能更好?

[英]Angular Reactive Forms vs ngModel, which has better performance?

I am trying to understand which of the two have better performance, I guess the type of form is important for the response, in my case I am trying to create some ControlValueAccessor components that contain a mat-autocomplete field each, the goal is to use it inside a bigger reactive form element, I would like to use the reactive form implementation in the ControlValueAccessor components but I don't know if it negatively affects performance and maybe it is better to have ngModel inside the reactive form我试图了解两者中哪一个具有更好的性能,我猜表单的类型对响应很重要,就我而言,我正在尝试创建一些 ControlValueAccessor 组件,每个组件都包含一个 mat-autocomplete 字段,目标是使用它在一个更大的响应式表单元素中,我想在 ControlValueAccessor 组件中使用响应式表单实现,但我不知道它是否会对性能产生负面影响,也许在响应式表单中使用 ngModel 会更好

Angular Forms Angular Forms

There is no noticeable difference in performance.性能没有明显差异。 You shouldn't base your choice on it.你不应该以此为基础做出选择。

Template Forms模板 Forms

Template forms, while easy to use, are rather difficult to unit test.模板 forms 虽然易于使用,但很难进行单元测试。 Angular will just generate the model for you. Angular 将为您生成 model。 This is done during transpiling, so it does not affect the performance in production.这是在转译期间完成的,因此不会影响生产中的性能。

Model Forms Model Forms

Compared to template forms, model forms (or reactive forms) are easier to test in your unit tests.与模板 forms 相比,model forms(或反应形式)更容易在单元测试中进行测试。 You will have to create the model yourself.您必须自己创建 model。 This leads to more code in your component.ts file.这会导致您的 component.ts 文件中有更多代码。

My Thoughts我的想法

In other words, if you plan to test or have special needs for your form, you might want to use reactive forms.换句话说,如果您计划测试或对表单有特殊需求,您可能希望使用反应式 forms。

If you are not planning to write tests, your needs for the form are rather basic or the overall skill of using angular in your team is rather low, maybe pick the simple solution and use template forms.如果您不打算编写测试,您对表格的需求比较基本,或者您的团队使用 angular 的整体技能比较低,可以选择简单的解决方案并使用模板 forms。

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

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