我使用visual studio模板创建了ionic 2 app,并试图在Azure Mobile Apps功能中开发离线数据同步 . 我在app.components.ts中安装了节点"module of azure-mobile-apps-client"并使用 import * as WindowsAzure from 'azure-mobile-apps-client'; 并使用 Client = new WindowsAzure.MobileServiceClient("url"); 初始化存储,但错误显示为"TypeError:Cannot read property 'openDatabase' of undefined" .

我还安装了@ ionic-native / sqlite节点模块和cordova-sqlite-storage插件 .

请看下面的代码:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { NavController } from 'ionic-angular';
//declare var WindowsAzure: any;
import * as WindowsAzure from 'azure-mobile-apps-client';

var mobileAppClient, // Connection to the Azure Mobile App backend
    store,  // Sqlite store to use for offline data sync
    syncContext, // Offline data sync context
    tableName
var useOfflineSync: boolean = true;


@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})
export class AboutPage {

    constructor(public navCtrl: NavController, public platform:Platform) {

        platform.ready().then(() => {
            mobileAppClient = new WindowsAzure.MobileServiceClient("https://myapp.azurewebsites.net");
            // Create the sqlite store
            store = new WindowsAzure.MobileServiceSqliteStore('store.db');

            store.defineTable({
                name: 'todoitem',
                columnDefinitions: {
                    id: 'string',
                    text: 'string',
                    complete: 'boolean',
                    version: 'string'
                }
            });

            // Get the sync context from the client
            syncContext = mobileAppClient.getSyncContext();

            // Initialize the sync context with the store\
            syncContext.initialize(store).then((syc) => {

                // Get the local table reference.
                tableName = mobileAppClient.getSyncTable('todoitem');

                // Sync local store to Azure table when app loads, or when login complete.
                syncContext.push().then((res) => {

                    // Push completed
                    // Pull items from the Azure table after syncing to Azure.
                    syncContext.pull(new WindowsAzure.Query('todoitem')).then((data) => {
                        alert(JSON.stringify(data));
                    }, (err) => {
                        alert(err);
                    });

                });

            }, function (err) {
                alert(err);
            });
        });

  }

}