Test code:
import { of } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export class JerrySandBoxService{
name = 'Jerry';
print(){
const myObservable = of(1, 2, 3);
// 创建一个观察者对象-Observer(处理next、error、complete回调)
const myObserver = {
next: x => console.log('Observer got a next value: ' + x),
error: err => console.error('Observer got an error: ' + err),
complete: () => console.log('Observer got a complete notification'),
};
// 通过Observable的subscribe函数,观察者去订阅可观察者的消息
myObservable.subscribe(myObserver);
}
}
The Observable constructor is called, and the incoming array is wrapped by subscribeToArray:
Observable's constructor, the accepted parameter is another function:
Finally, of returns an observable object.
To get more original articles by Jerry, please follow the public account "Wang Zixi":