0003 - Adopt Observable Data Services for Angular
最后更新于
最后更新于
class OrganizationService {
private _organizations: new BehaviorSubject<Organization[]>([]);
organizations$: Observable<Organization[]> = this._organizations$.asObservable();
async save(organizations: { [adr: string]: OrganizationData }) {
await this._organizations$.next(await this.decryptOrgs(this._activeAccount, organizations));
}
}
class Component implements OnDestroy {
private destroy$: Subject<void> = new Subject<void>();
ngInit() {
this._organizationService.organizations$
.pipe(takeUntil(this.destroy$))
.subscribe((orgs) => {
this.orgs = orgs;
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.unsubscribe();
}
}