简要说明:在我下面的代码中,ngOnInit()
内的opn.success = function()
在
console.log(“此行首先执行”);
而我们要先执行opn.onsuccess = function()
。
请帮助。
ngOnInit()
{
var opn = indexedDB.open("webDealer", this._globalvar.getvarDBVersion());
opn.onsuccess = function() {
var db = opn.result;
var transac = db.transaction(['usrprfl'], 'readonly');
var storeNm = transac.objectStore('usrprfl');
var countRequest = storeNm.count();
countRequest.onsuccess = function()
{
this.cnt = countRequest.result;
if(this.cnt == 1 )
{
storeNm.getAll().onsuccess = function(event)
{
var rslt = event.target.result;
this.usr = rslt[0].nm;
this.pwd = rslt[0].pw;
this.clicktyp ='AUTO';
};
}
else
{
this.clicktyp ='MANUAL';
}
};
db.close();
};
console.log("This line is executed first");
}
OnLogon()
{
//... mycode
}
答案 0 :(得分:0)
对indexedDB.open的调用异步解析。如果希望仅在数据库打开后才显示该日志消息,则需要等待直到数据库打开。现在,您的代码不会等待。
下面是一些等待时间的示例代码:
function open(name, version) {
return new Promise((resolve, reject) => {
var request = indexedDB.open(name, version);
request.onsuccess = _ => resolve(request.result);
request.onerror = _ => reject(request.error);
});
}
async function ngOnInit() {
var db = await open('webDealer', this._globalvar.getvarDBVersion());
console.log("This line is no longer executed first");
}