What is pipe() function in Angular

前端 未结 5 1581
时光取名叫无心
时光取名叫无心 2020-12-12 13:10

Pipes are filters for transforming data (formats) in the template.

I came across the pipe() function as below. What does this pipe() functi

5条回答
  •  萌比男神i
    2020-12-12 14:01

    The Pipes you are talking about in the starting description are different from the pipe you showed in the example.

    In Angular(2|4|5) Pipes are used to format view as you said. I think you have a basic understanding of pipes in Angular, you can learn more about that from this link - Angular Pipe Doc

    The pipe() you have shown in the example is the pipe() method of RxJS 5.5 (RxJS is the default for all Angular apps). In Angular5 all the RxJS operators can be imported using single import and they are now combined using the pipe method.

    tap() - RxJS tap operator will look at the Observable value and do something with that value. In other words, after a successful API request, the tap() operator will do any function you want it to perform with the response. In the example, it will just log that string.

    catchError() - catchError does exactly the same thing but with error response. If you want to throw an error or want to call some function if you get an error, you can do it here. In the example, it will call handleError() and inside that, it will just log that string.

提交回复
热议问题