Ionic 2 存储无法在 iOS 上运行
Ionic 2 storage not working on iOS
Ionic 2 存储在 ios 上不工作,但在 Android 上工作。我正在使用这个 import { Storage } from '@ionic/storage';
在本地存储数据。
我做了这样的样品
import { Storage } from '@ionic/storage';
export class MyApp {
constructor(private storage: Storage) { }
setData() {
// set a key/value
storage.set('username', 'johndoe');
}
getData() {
// get a key/value pair
storage.get('username').then((val) => {
alert('Your username is', val);
});
}
}
它在 Android 上完美运行。但是当我尝试在 iOS 上构建时,它不起作用。数据未显示。关于如何在 ionic 2 中存储持久数据的一些建议,它在 android 和 iOS 平台上完美运行。
如果您将安装 SQLite 插件,那么您在 iOS 上也不会有任何问题。
这是 SQLite 插件。
When running in a native app context, Storage will prioritize using
SQLite, as it's one of the most stable and widely used file-based
databases, and avoids some of the pitfalls of things like localstorage
and IndexedDB, such as the OS deciding to clear out such data in low
disk-space situations.
首先,我没有看到您的主模块定义,所以我会先谈谈这一点。在您的主应用程序模块中,您需要为 IoinicStorageModule
导入,然后您需要将其放入 imports
部分:
imports:[IonicStorageModule.forRoot({'yourappname'})]
其次,我没有看到您检查存储是否准备就绪,您可能也应该这样做:
this.storage.ready()
.then(() => {
this.storage.set(key, value);
});
最后,您的本地存储问题可以通过这个简单的服务轻松解决:
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class LocalStorageService {
constructor(private storage: Storage){
}
public setValue(key: string, value: string): Promise<boolean> {
return this.storage.ready()
.then(() => {
return this.storage.set(key, value)
.catch(() => {
return null;
});
});
//TODO: Handle storage not being available
}
public getValue(key: string): Promise<string> {
return this.storage.ready()
.then(() => {
return this.storage.get(key)
.catch(() => {
return null;
});
});
//TODO: Handle storage not being available
}
}
我每天都用这个。不要忘记围绕它包装一些单元测试。
Ionic 2 存储在 ios 上不工作,但在 Android 上工作。我正在使用这个 import { Storage } from '@ionic/storage';
在本地存储数据。
我做了这样的样品
import { Storage } from '@ionic/storage';
export class MyApp {
constructor(private storage: Storage) { }
setData() {
// set a key/value
storage.set('username', 'johndoe');
}
getData() {
// get a key/value pair
storage.get('username').then((val) => {
alert('Your username is', val);
});
}
}
它在 Android 上完美运行。但是当我尝试在 iOS 上构建时,它不起作用。数据未显示。关于如何在 ionic 2 中存储持久数据的一些建议,它在 android 和 iOS 平台上完美运行。
如果您将安装 SQLite 插件,那么您在 iOS 上也不会有任何问题。
这是 SQLite 插件。
When running in a native app context, Storage will prioritize using SQLite, as it's one of the most stable and widely used file-based databases, and avoids some of the pitfalls of things like localstorage and IndexedDB, such as the OS deciding to clear out such data in low disk-space situations.
首先,我没有看到您的主模块定义,所以我会先谈谈这一点。在您的主应用程序模块中,您需要为 IoinicStorageModule
导入,然后您需要将其放入 imports
部分:
imports:[IonicStorageModule.forRoot({'yourappname'})]
其次,我没有看到您检查存储是否准备就绪,您可能也应该这样做:
this.storage.ready()
.then(() => {
this.storage.set(key, value);
});
最后,您的本地存储问题可以通过这个简单的服务轻松解决:
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class LocalStorageService {
constructor(private storage: Storage){
}
public setValue(key: string, value: string): Promise<boolean> {
return this.storage.ready()
.then(() => {
return this.storage.set(key, value)
.catch(() => {
return null;
});
});
//TODO: Handle storage not being available
}
public getValue(key: string): Promise<string> {
return this.storage.ready()
.then(() => {
return this.storage.get(key)
.catch(() => {
return null;
});
});
//TODO: Handle storage not being available
}
}
我每天都用这个。不要忘记围绕它包装一些单元测试。