方法与管道 [英] Methods vs pipes

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

问题描述

在Angular应用程序中使用管道和模板插值方法之间有区别吗?

Is there a difference between using a pipe and a method in template interpolation in an Angular application?

例如:

< h1> {{name.toLowerCase()}}</h1> < h1> {{name |小写}}</h1>

就表现而言,是真正的收获还是仅仅是个人喜好?

In terms of performance, is there a real gain or is it just personal preference?

我知道您的模板中的调用方法通常会降低性能,这是由于Angular不断检查其执行是否发生了任何更改.大多数时候,我会在组件上使用计算属性.

I know that calling methods in your template will generally slow performance due to Angular constantly checking to see whether or not its execution has changed anything. Most of the time, I'd use a computed property on my component.

推荐答案

TL; DR; 不要在模板中使用函数或方法,而应使用管道.

TL;DR; Don't use functions or methods in the template, use pipes instead.

仅当输入值更改时,才会调用管道.在每次更改检测时都会调用一个函数或方法.如果您想了解有关模板中函数的更多信息,这是一篇不错的文章.

A pipe would be called only when input values change. A function or a method would be called on every change detection. Here is a nice article if you want to know more about functions in template.

这是方法vs管道的运行中stackblitz演示.

这篇关于方法与管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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