小谈:
上一篇:Angular公共组件开发谈了Angular公共组件的开发,其中有个细节不知道大家注意到没有。
异步传输数据
异步传输的方法在Angular里很常见,
async ngOnInit():Promise<void> {
do something...
await this.another();
//another执行完后开始执行
this.then();
}
下面我们就谈谈三种传播异步数据到组件的方式。
第一种
使用 *ngIf
第二种
就是上面用的 ngOnchanges()方法。
第三种
利用RxJs BehaviorSubject
不了解这个方法的戳这里.
只要是先 new 一个 BehaviorSubject,
private _data = new BehaviorSubject<any[]>([]);
然后用get()和set()方法监测变化值.
// initialize a private variable _data, it's a BehaviorSubject
private _data = new BehaviorSubject<Post[]>([]);
// change data to use getter and setter
@Input()
set data(value) {
// set the latest value for _data BehaviorSubject
this._data.next(value);
};
get data() {
// get the latest value from _data BehaviorSubject
return this._data.getValue();
}
之后在OnInit()里处理就行了
ngOnInit() {
// now we can subscribe to it, whenever input changes,
this._data
.subscribe(x => {
this.dataSource = x;
});
}
如果想取一次值之后就解除订阅,可以用
ngOnInit() {
this._data
// add this line
// listen to data as long as groupPosts is undefined or null
// Unsubscribe once groupPosts has value
.takeWle(() =>!this.dataSourceSubscription)
.subscribe(x => {
this.dataSourceSubscription=this.xxx();
});
}
.takeWhile(() => !this.dataSourceSubscription)
这个方法是订阅之后立马取消订阅。
就说这么多吧,欢迎大家指正。