知识搬运工,原视频链接:www.youtube.com/watch?v=vtC…
之前看过一些rxjs的资料,理解起来很有难度,看了这个视频有很大的帮助。
本文将视频中的核心概念记录下来。建议先学习原视频,然后本文作为资料来复习查看
看完视频,你将学习到:
- 什么是Observable
- Observable做了什么
- Subscription Observable Observer之间的关系以及示例
- 什么是Pipe,怎么使用Pipe
- 常用的四种rxjs pattern模式,结合一个Angular应用示例来看
RxJS Patterns 模式
- Declarative Data Access Pattern
- Retrieve on Action Pattern
- Shape on Action Pattern
- Retrieve Related Data Pattern
Terms术语
术语1 Observable
Observable is a colleaction of items over time.
Why Use an Observable?
What Does an Observable Do?
It does nothing until a consumer subscribes
When subsribed, the Observable begins emitting items or notifications to the consumer: next: The next item is emitted error: An error occurred and no more items are emitted complete: No more items are emitted
Angular Creates Observables
An Observable does nothing until a consumer subscribes
术语二 Subscription
Tip: Ensure each Observable is subscribed Ensure each subscription is unsubscribed
确保unsubsribe,以免内存泄漏
术语三 Observer
Oberver watches for emissions and notifications: next error complete
Observer is an object with three methods
通常不这样写:
箭头函数observer,放在subscribe里
Observable, Subscription, Observer
Pipe 管道
when you work with rxjs, you can specify a pipe which transforms each emitted item.
pipe it through a lowercase operator and it becomes a lowercase a, we can pipe that same a through an enlarged operator and we have a bigger lowercase a. That is how piping works, it takes in the emitted value, it performs some operation on it and then it emits to the next operator in the pipeline.
Common Piperable RxJS Operators
Using Pipe
start the stream with a subscribe, then we pipe through a set of operators
no need for an observer, because we can process the items instead through these pipable operators.
RxJS模式 Patterns
介绍4种RxJS模式
what you need to do to make your rxjs pipelines work, answering these three questions.
Declarative Data Access Pattern
Tip: use the async pipe
Retrive on Action Pattern
做一个下拉框查询,
听到action时,就要使用Subject或者BehaviorSubject了