Angular async pipe在html模板中的一个实际应用

时间:2022-07-26
本文章向大家介绍Angular async pipe在html模板中的一个实际应用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

看个例子:

<cx-carousel
  [items]="items$ | async"
  [title]="title$ | async"
  [template]="carouselItem"
  itemWidth="285px"
>
</cx-carousel>

title$是消费cx-carousel的Component传入的Observable对象:

title

的数据源,componentData

, 也是一个Observable,来自ComponentData.data$调用pipe后的结果:

运行时title$的值通过async pipe取出:

operator里能看到map操作:

这个Observable是通过另一个Observable执行filter操作得到的:

这个source指向的Observable就是componentData.data$:

async pipe的实现类是AsuncPipe,实现了PipeTransform接口的方法:

transform方法里执行subscribe, 触发Observable的执行: