简体   繁体   English

在 Angular (^11) 中构建可重用的自定义和动态过滤/排序的最佳实践是什么

[英]What is considered the best practice to build reusable custom and dynamic filtering / ordering in Angular (^11)

I want to add custom ordering and filtering functions to my app and would like them to be reusable (across multiple components).我想向我的应用程序添加自定义排序和过滤功能,并希望它们可重用(跨多个组件)。 The said functions will be used to allow users to order list/table contents and filter arrays in my templates.上述功能将用于允许用户订购列表/表格内容并在我的模板中过滤 arrays。 In addition, some of the content can be dynamically modified following a user's actions (ex.: if, through our database, a remote user adds a new element to the collection listed in a table, the updated data on the local user should respect the previously applied ordering/filtering).此外,某些内容可以根据用户的操作进行动态修改(例如:如果远程用户通过我们的数据库将新元素添加到表中列出的集合中,则本地用户的更新数据应该尊重以前应用的排序/过滤)。 It is my understanding that pipes wouldn't really be the best option in such cases since changes of the sort are impure .据我了解,管道在这种情况下并不是最好的选择,因为这种变化是不纯的

With a pure pipe, Angular ignores changes within composite objects, such as a newly added element of an existing array, because checking a primitive value or object reference is much faster than performing a deep check for differences within objects.对于纯 pipe,Angular 会忽略复合对象内的变化,例如现有数组的新添加元素,因为检查原始值或 object 引用比执行深入检查对象内的差异要快得多。 Angular can quickly determine if it can skip executing the pipe and updating the view. Angular 可以快速判断是否可以跳过执行 pipe 和更新视图。

Is my understanding of pure/impure changes mistaken?我对纯/不纯变化的理解是错误的吗? If not, what would be a good alternative to implement reusable orderBy/filterBy functions?如果不是,那么实现可重用 orderBy/filterBy 函数的好选择是什么?

You are correct, if you have an array, and its elements are changed, a pure pipe will not update if the array's reference stays the same.你是对的,如果你有一个数组,并且它的元素发生了变化,那么如果数组的引用保持不变,纯 pipe 将不会更新。

I recommend you consider a reactive approach, using a BehaviorSubject / Observable and the async pipe to emit data to your view.我建议您考虑一种反应式方法,使用BehaviorSubject / Observableasync pipe 将数据发送到您的视图。 This will trigger change detection whenever new data is emitted, and you are free to use pure pipes to filter your data.每当发出新数据时,这将触发更改检测,并且您可以自由使用纯管道来过滤数据。

Here is a solid article about using the async pipe. 这是一篇关于使用async pipe 的可靠文章。

impure pipes wouldn't be performant, but pure pipes would be, so it is the best approach, yes Angular wouldn't detect composite changes, that's why immutability is great.不纯管道不会有性能,但纯管道会,所以这是最好的方法,是的 Angular 不会检测到复合变化,这就是不变性很棒的原因。

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

相关问题 在Angular 2中tslint的成员顺序是什么以及实现的最佳实践 - What is member-ordering of tslint in Angular 2 and the best practice of implementation 在Angular 2中过滤数组的最佳实践 - Best practice for filtering an array in Angular 2 从Angular 2中的动态数组生成HTML的最佳实践是什么 - What is the best practice to generate HTML from dynamic array in Angular 2 Angular 中自定义验证的最佳实践 - Best Practice for Custom Validations in Angular 可重复使用的angular 11模板 - Reusable angular 11 template 将自定义Modernizr构建集成到Angular项目的最佳方法是什么? - What is the best way to integrate custom Modernizr build into an Angular project? 在angular 7中动态html引导程序表中实现排序和过滤的最佳方法是什么? - What is the best way to implement sorting and filtering in a dynamic html bootstrap table in angular 7? angular 中订阅方法的最佳实践是什么? - What is the best practice for subscribe method in angular? 在 Angular 8 中监听 DOM 事件的最佳实践是什么? - What is the best practice to listening to DOM event in Angular 8? 以角度返回错误时的最佳做法是什么 - What is the best practice when returning errors in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM