我有具有此功能的DBService typescript类
`initialiseDb(): Observable < any > {
let source = Observable.create((observer) =>
{}
openRequest.onsuccess = (event: any) =>
{
console.log("Database open");
this.localDb = event.target.result;
}
observer.next();
observer.complete();
});
return source;
}
在ngOnInit上的app.component.ts
let dbInitObservable = this.localDb.initialiseDb().subscribe
((res => alert("Initilisation")), err => alert("Error"), () => this.authService.autoSignIn());
我需要在autoSignIn
被调用并完成后仅在authService
完成后致电initialiseDb
。
任何想法?,没有语法错误,但this.authService.autoSignIn()永远不会被调用。
的Ta。
答案 0 :(得分:0)
你需要在传递给open()
的回调中调用数据库Observable.create()
,然后你需要监听数据库并发出正确的下一个错误并从监听器中完成。
initialiseDb(): Observable<any> {
return Observable.create((observer) => {
let openRequest = localDatabase.indexedDB.open(dbName);
openRequest.onsuccess = (event: any) => {
console.log("Database open");
this.localDb = event.target.result;
observer.next(this.localDb);
observer.complete();
}
openRequest.onerror = (event: any) => {
console.log("Database error: " + e.target.errorCode);
observer.error(e.target.errorCode);
}
})
}