首页 > 解决方案 > 无法打开sqlite数据库离子框架

问题描述

我从 ionic framework doc 添加了这个插件

> ionic cordova plugin add cordova-sqlite-storage <br>npm install
> @ionic-native/sqlite

在我的应用程序 module.ts 我导入并注入到提供程序

> import { *SQLite*, SQLiteObject } from '@ionic-native/sqlite/ngx';<br><br>
> providers: [
   StatusBar,
    SplashScreen,
>     { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },SQLite,Allconnection],

在 mymainpage.ts

import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
 import {Platform} from '@ionic/angular';
 declare var window;



 constructor
    (private sqlite: SQLite,public platform: Platform){                                                    
       this.platform.ready().then (()=>{
      var db = window.sqlitePlugin.openDatabase({ name: 'my.db', location: 'default' }, function (db) {
  }, function (error) {
      console.log('Open database ERROR: ' + JSON.stringify(error));
  });
})
 }

当我在 chrome 浏览器上运行它时出现此错误

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'openDatabase' of undefined
TypeError: Cannot read property 'openDatabase' of undefined

标签: angularionic-framework

解决方案


尝试像这样将 SQLite 移动到 providers 数组中。

> import { *SQLite*, SQLiteObject } from '@ionic-native/sqlite/ngx';<br><br>
> providers: [
   StatusBar,
   SQLite
   SplashScreen,
>     { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },Allconnection],

每次添加离子原生插件时,您都必须确保将 npm 模块添加到提供程序列表中。


推荐阅读