【问题标题】:sqlitePlugin Error while using SQLite in ionic 2在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误
【发布时间】:2017-05-10 21:28:17
【问题描述】:

我正在尝试实现 ionic 2 doc 中给出的这个简单示例: http://ionicframework.com/docs/v2/native/sqlite/

我在MAC 上尝试了该示例(对放置在离子项目的'www\test.sqlite' 文件夹下的数据库执行查询),我在浏览器和iOS 模拟器上都收到此错误(在设备上也不起作用):

ReferenceError: sqlitePlugin 未定义

我在 ionic 项目中添加了 cordova-sqlite-storage 插件。

代码:

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);
        });
    }

}

离子详细信息:Cordova CLI:6.4.0 离子框架版本:2.0.0-rc.3 Ionic CLI 版本:2.1.17 Ionic App Lib 版本:2.1.7 Ionic App 脚本版本:0.0.45 ios-deploy 版本:未安装 ios-sim 版本:未安装操作系统:OS X El Capitan 节点版本:v7.2.1 Xcode 版本:Xcode 8.1 构建版本 8B62

【问题讨论】:

    标签: android ios sqlite cordova angular


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您需要添加下一个细节:

      在您的导入中:

      import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
      

      在getSummary()函数中:

      getSummary(){
          let db = new SQLite();
          db.openDatabase({
              name: 'test.sqlite',
              location: 'default' // the location field is required
          }).then((sqlite: SQLiteObject) => {
      

      我希望这能解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 2017-10-21
        • 2017-08-08
        • 2016-06-21
        • 1970-01-01
        • 2018-05-01
        • 2016-02-28
        相关资源
        最近更新 更多