Typescript Angular2,Ionic2:如何在手机中存储数据并检索数据?

Typescript Angular2,Ionic2:如何在手机中存储数据并检索数据?,typescript,mobile,ionic2,local-storage,angular2-services,Typescript,Mobile,Ionic2,Local Storage,Angular2 Services,我正在使用Angular2和Ionic2框架开发一个移动应用程序。该应用程序基本上是一个考试应用程序,要求用户使用该应用程序参加考试。因此,我的要求是,它应该工作,甚至离线,即没有互联网连接。我在寻找可能的选择,发现我们可以将考试内容存储在用户的设备上。但我想知道我该怎么做 提前谢谢。非常感谢您的帮助。您可以使用本机存储,请查阅爱奥尼亚文档。 您需要安装Cordova插件,导入它,将其添加到构造函数中,然后您可以使用它来保存信息 this.nativeStorage.setItem('myit

我正在使用Angular2和Ionic2框架开发一个移动应用程序。该应用程序基本上是一个考试应用程序,要求用户使用该应用程序参加考试。因此,我的要求是,它应该工作,甚至离线,即没有互联网连接。我在寻找可能的选择,发现我们可以将考试内容存储在用户的设备上。但我想知道我该怎么做


提前谢谢。非常感谢您的帮助。

您可以使用本机存储,请查阅爱奥尼亚文档。

您需要安装Cordova插件,导入它,将其添加到构造函数中,然后您可以使用它来保存信息

this.nativeStorage.setItem('myitem', {property: 'value', anotherProperty: 'anotherValue'})
    .then(
        () => console.log('Stored item!'),
        error => console.error('Error storing item', error)
     );
这是用来获取信息的

this.nativeStorage.getItem('myitem')
    .then(
        data => console.log(data),
        error => console.error(error)
     );

但请参考链接并查看官方文档。

您可以使用nativeStorage,但它只能在cordova平台上工作(使用ionic run或ionic emulate,但不使用ionic serve)

我的解决方案是使用ionic g provider创建一个StorageProvider,其中我创建了方法:“setItem,getItem”

从'@angular/core'导入{Injectable,Component};
从'@angular/Http'导入{Http};
从“离子角度”导入{Platform};
从'@ionic native/native storage'导入{NativeStorage}
导入'rxjs/add/operator/map';
/*
为StorageProvider生成的类。
看见https://angular.io/docs/ts/latest/guide/dependency-injection.html
有关提供者和Angular 2 DI的更多信息。
*/
@可注射()
@组成部分({
提供者:[平台,本地存储]
})
导出类存储提供程序{
构造函数(公共http:http,私有平台:平台,私有nativeStorage:nativeStorage){
log(“Hello StorageProvider Provider”);
}
设置存储(storageId:string,data:Object){
返回新承诺((解决、拒绝)=>{
如果(此.platform.is('cordova')){
返回这个.nativeStorage.setItem(storageId,data)。然后(res=>{
决议(res);
}).catch(错误=>{
拒绝(错误);
});
}否则{
解析(localStorage.setItem(storageId、JSON.stringify(数据)))
}
})
}
getStorage(storageId:string){
返回新承诺((解决、拒绝)=>{
如果(此.platform.is('cordova')){
this.nativeStorage.getItem(storageId).then(res=>{
决议(res);
}).catch(错误=>{
拒绝(错误);
})
}否则{
解析(localStorage.getItem(storageId))
}
})
}
}

非常感谢您提供的信息。但我想知道是否可以存储JSON格式的内容。我还想知道如何保存用户的答案。我应该让用户下载考试并参加考试吗?我有大约200个问题。我还能用Nativestorage吗。我读过一些关于文件插件的内容。这有什么用吗?是的,您使用setStorage函数保存JSON对象,例如,您需要这样做来保存一个检查。setItem(“physicsExam”,{“问题”:[]};然后,当您想要获得它时,您可以在执行storageProvider.getItem(“physicsExam”)时四处搜索但我不理解第二个问题。我的疑问是,是否可以存储包含多达200个问题的考试。我应该让用户在尝试之前下载考试,还是应该怎么做?我很抱歉有很多疑问。但我对这个概念完全陌生。非常感谢您的回答。非常感谢您提供的信息上。但我想知道是否可以存储JSON格式的内容。我还想知道如何保存用户的答案。我应该让用户始终下载考试并参加考试吗?我的问题也是以对象的形式出现的。使用本机插件可以吗?是的,您可以保存JSON,
'myitem',{property:'value',anotherProperty:'anotherValue'}
。请解释更多关于第二个问题的信息。我是否可以让用户在尝试之前下载考试,或者应该如何完成?用户给出的答案会与考试一起保存在本机存储中吗?我很抱歉有很多疑问。但我对这一概念完全陌生。非常感谢。这取决于您希望实现什么目标e、 您可以下载考试问题并将其与NativeStore一起保存,并带有考试id。您可以毫无问题地拥有一个相当大的json!我想要实现的是,用户应该能够参加考试,即使他/她开始考试后没有互联网连接。假设用户失去了他/她应该进行的连接我可以参加考试,答案应该存储在某个地方,直到互联网连接恢复后在服务器上更新。我想知道什么时候应该让用户下载考试。
import { Injectable, Component } from '@angular/core';
import { Http } from '@angular/http';
import { Platform } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage'


import 'rxjs/add/operator/map';

/*
  Generated class for the StorageProvider provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
@Component({
  providers: [Platform, NativeStorage]
})
export class StorageProvider {

  constructor(public http: Http, private platform: Platform, private nativeStorage: NativeStorage) {
    console.log('Hello StorageProvider Provider');
  }

  setStorage(storageId: string, data: Object){
    return new Promise<any>((resolve, reject)=>{
      if (this.platform.is('cordova')) {
        return this.nativeStorage.setItem(storageId, data).then(res=>{
          resolve(res);
        }).catch(err=>{
          reject(err);
        });
      } else {
        resolve(localStorage.setItem(storageId, JSON.stringify(data)))
      }
    })
  }

  getStorage(storageId: string){
    return new Promise<any>((resolve, reject)=>{
      if (this.platform.is('cordova')){
          this.nativeStorage.getItem(storageId).then(res=>{
            resolve(res);
          }).catch(err=>{
            reject(err);
          })
      } else {
        resolve(localStorage.getItem(storageId))
      }
    })
  }

}