OGeek|极客世界-中国程序员成长平台

标题: android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:25
标题: android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误

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



Best Answer-推荐答案


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

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4