android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误

标签 android ios sqlite cordova angular

I'm trying to implement this simple example given in ionic 2 doc: http://ionicframework.com/docs/v2/native/sqlite/

我在 MAC 上尝试了这个例子(在 ionic 项目的 'www\test.sqlite' 文件夹下的数据库上执行查询),我都得到了这个错误在浏览器和 iOS 模拟器上(也不适用于设备):

ReferenceError: sqlitePlugin 未定义

我已将 cordova-sqlite-storage 插件添加到 ionic 项目中。

代码:

constructor(public navCtrl: NavController, public platform: Platform,
                public pps: ProdPerfService){
        platform.ready().then((readySource) => {
            pps.getSummary(); //pps is a provider named ProdPerfService
        });
    }

//ProdPerfService:
import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';

@Injectable()
export class ProdPerfService {
    constructor(){

    }

    getSummary(){
        let db = new SQLite();
        db.openDatabase({
            name: 'test.sqlite',
            location: 'default' // the location field is required
        }).then(() => {
            db.executeSql('select * from summary', {}).then(() => {
                alert('result');

        }, (err) => {
            console.error('Unable to execute sql: ', err);
            alert('err');
        })
        }, (err) => {
            console.error('Unable to open database: ', err);
            alert(err);
        });
    }

}

ionic details: Cordova CLI: 6.4.0 Ionic Framework Version: 2.0.0-rc.3 Ionic CLI Version: 2.1.17 Ionic App Lib Version: 2.1.7 Ionic App Scripts Version: 0.0.45 ios-deploy version: Not installed ios-sim version: Not installed OS: OS X El Capitan Node Version: v7.2.1 Xcode version: Xcode 8.1 Build version 8B62

最佳答案

尝试将您的逻辑移出构造函数。

ionViewDidLoad(){
   this.platform.ready().then((readySource) => {
       this.pps.getSummary(); //pps is a provider named ProdPerfService
   });
}

关于android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41330841/

相关文章:

ios - UIImage 性能 : imageNamed vs UIGraphicsGetImageFromCurrentImageContext from bezierPath

ios - 计算嵌套在另一个结构中的结构数?

java - SQLite + 可序列化、SQLite + GSON,还是只是适用于我的 Android 应用程序的普通 SQLite?

objective-c - SQLite 数据显示

android - Google Cloud Messaging 可以与 Facebook 一起使用吗?

android - 如何以特定的开始时间启动计时器?

java - 需要使用数组列表中的点绘制路径

java - @SuppressWarnings ("deprecation") 和 ("unused") 在 Java 中是什么意思?

javascript - 在 Obj C 中捕获 React Native 错误

android - SQLite 查询未按特定顺序执行