【问题标题】:Ionic 3 - sqlite shows error of "can not read property of openDatabase" when run with --prod flagIonic 3 - 使用 --prod 标志运行时,sqlite 显示“无法读取 openDatabase 的属性”的错误
【发布时间】:2018-02-27 02:47:25
【问题描述】:

我在 ionic 3 项目中使用cordova-sqlite-ext。我的代码在没有--prod 标志的情况下运行良好。

但是当我运行 ionic cordova run android --prod --relase 时,它显示“无法读取 openDatabase 的属性”

这是我的代码

  export class SqlLiteServiceProvider {
  private options = {name: 'db.db', location: 'default',existingDatabase: 1,createFromLocation:1};
  public win = (<any>window);
  private db : any;

  constructor() {
    try{
      if(this.win.cordova){
        this.db = this.win.sqlitePlugin.openDatabase(this.options);

      }else{
        this.db = this.win.openDatabase(this.options.name, '1.0', 'database', 5 * 1024 * 1024);
      }
  }catch(ex){
    alert(ex); // always alert error when run with --prod flag
  }
  }
}

【问题讨论】:

    标签: angular sqlite cordova typescript ionic3


    【解决方案1】:

    据我所知,当您进行生产构建时,window 对象的处理方式有所不同。当使用 ionic-native 中未包含的 cordova-plugins 时,您需要做一些不同的事情。

    告诉 typescript 你确定你的插件句柄存在于构建之后的某个地方:

    declar var SQLitePlugin; // add this at the same level as imports
    

    例如在ngOnInit()中使用它:

    ngOnInit() {
      SQLitePlugin.openDatabase(...);
    }
    

    但我强烈建议您不要这样做,而是使用@ionic/storage@ionic-native/sqlite

    【讨论】:

      【解决方案2】:

      您不能在调试模式下运行ionic cordova run android --prod --relase。它用于 google play 商店发布版本。如果您需要在您的设备上进行测试,您应该在CLI 下方运行。

      ionic cordova run android --prod --device
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        • 2018-06-19
        • 2018-06-13
        • 2019-03-10
        • 2018-07-01
        • 2018-06-10
        • 2015-03-30
        相关资源
        最近更新 更多