Ionic2错误属性' openDatabase'在类型' Window'上不存在

时间:2018-04-21 10:00:51

标签: javascript cordova ionic-framework ionic2 ionic3

我试图为ionic2 / 3创建一个回退数据库,成为websql,但我被卡住了

private db:any;
constructor(
    private storage: SQLite,
    private platform: Platform,
    private windowserv:WindowServiceProvider
) {
    if (this.platform.is('core') || this.platform.is('mobileweb')) {
        this.db=window.openDatabase(this.db_name, "1.0", "Database", 2 * 1024 * 1024);
    }
}


 openSqliteDb(): Promise<any> { //returns the db object
    return new Promise<any>((resolve, reject) => {
        if (this.platform.is('core') || this.platform.is('mobileweb')) {
            try {
                resolve(this.db);
            } catch (e) {
                reject(e);
            }
        } else {
            this.storage = new SQLite();
            this.storage.create({
                name: this.db_name,
                location: this.db_location
            }).then((db: SQLiteObject) => {
                resolve(db);
            }, (error) => {
                reject(error);
            });
        }
    })

}

但现在总是收到错误

Property 'openDatabase' does not exist on type 'Window'.

我尝试过添加窗口服务,引导就像

@Injectable()
export class WindowServiceProvider {
  public window = window;

}

然后在appmodule上

    bootstrap: [IonicApp,[WindowServiceProvider]],

然后将其用作

windowservice.window.openDatabase ....

但即使这样也行不通。 在使用Web进行测试时,如何使用open database for ionic websql fallback

1 个答案:

答案 0 :(得分:1)

window.openDatabase是一种科尔多瓦方法。所以它在浏览器中不起作用。 运行以下命令以使cordova可用

$ ionic run browser