2
我對混合應用程序使用Ionic 2(Angular 2)。我將一個共享提供程序注入到將顯示來自SQLite3數據庫的數據的頁面中,然後繼續加載數據。但是,在創建我的數據庫提供程序時,打開數據庫需要一些時間(很少)。但是,我的代碼(截至此時)不等待數據庫在查詢之前打開,這顯然會導致錯誤。Ionic2/Angular2等待SQLite數據庫在查詢之前打開
如何構建我的代碼,它將等待數據庫被打開以避免崩潰?
我的數據庫提供商的構造:
constructor(private platform: Platform) {
this.platform.ready().then(() => {
if(this.isOpen !== true) {
this.storage = new SQLite();
this.storage.openDatabase({name: "data.db", location: "default"}).then(() => {
this.isOpen = true;
this.storage.executeSql("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", []);
});
}
});
console.log('Hello Database Provider');
該供應商被注入到我的網頁的構造。
加載頁面(主頁)時觸發一個事件,該事件調用load()
函數。
ionViewDidLoad() {
this.load();
console.log('Hello Home Page');
負載的功能:
public load() {
this.database.getPeople().then((result) => {
this.itemList = <Array<Object>> result;
}, (error) => {
console.log("LOAD ERROR: ", error);
});
我非常希望有人能指出我在正確的方向:)
這裏有什麼消息嗎?我面臨同樣的問題。 – dermoritz
@dermoritz我解決了這個問題,並添加了我的解決方案作爲答案:) – enemypixel