Angular中的不纯管道是什么? [英] What is an impure pipe in Angular?

查看:74
本文介绍了Angular中的不纯管道是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Pipe({name:'myPipe', pure: false})

我无法理解不纯净的管道.

I am unable to understand impure pipes.

纯管道和不纯管道有什么区别?

What's the difference between pure and impure pipes?

请通过一个简单的基本示例进行解释.

Please explain with a simple and basic example.

推荐答案

仅当Angular检测到值或传递给管道的参数发生变化时,才会调用纯管道.

每个更改检测周期都会调用不纯管道,无论值或参数是否更改.

An impure pipe is called for every change detection cycle no matter whether the value or parameter(s) changes.

这与Angular未检测到的更改有关

This is relevant for changes that are not detected by Angular

  • 当您传递一个更改内容的数组或对象时(但仍然是相同的实例)
  • 当管道注入服务以访问其他值时,Angular无法识别它们是否已更改.

在这些情况下,您可能仍然希望执行管道.

In these cases you probably still want the pipe to be executed.

您应注意,不纯净的管道容易造成效率低下.例如,当将数组传递到管道中以进行过滤,排序...时,则可能会在每次运行更改检测(这通常在默认的 ChangeDetectionStrategy 设置中)经常发生时完成此工作该数组甚至可能没有更改.您的管道应尝试识别出这一点,例如返回缓存的结果.

You should be aware that impure pipes are prone to be inefficient. For example when an array is passed into the pipe to filter, sort, ... then this work might be done every time change detection runs (which is quite often especially with the default ChangeDetectionStrategy setting) event though the array might not even have changed. Your pipe should try to recognize this and for example return cached results.

这篇关于Angular中的不纯管道是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆