使用RxJS Observables来处理异步操作。在第一个方法中使用Observable在关键点发出通知,接着订阅该通知,以等待该方法完成后运行第二个方法。以下是一个示例代码:
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
export class ExampleComponent {
private notification: Subject = new Subject();
firstMethod(): void {
// Async operation
this.notification.next();
}
secondMethod(): void {
// Code to run after firstMethod is done
}
ngOnInit() {
// Subscribe to notification and run second method when it is received
this.notification.subscribe(() => {
this.secondMethod();
});
}
}