pra*_*wal 0 ionic-framework ionic4 capacitor
最近我从 Cordova 转向电容器存储 API。我有一个非常奇怪的问题。在第 1 页,我将 4 个值设置为本地存储,但是当我在另一页上访问这些值时,一个值显示为 null。
我尝试了很多方法,但值user_id始终为空。
我确保价值存储在本地存储中。
在从本地存储设置和检索值后,我还附加了 console.log 的图像
存储服务.ts
async set(key: string, value: any): Promise<any> {
if(value == null || value == undefined) {
console.log("dont'do")
}
else {
try {
await Storage.set({
key: key,
value: value
});
return true;
} catch (reason) {
console.log(reason);
return false;
}
}
}
// to get a key/value pair
async get(key: string): Promise<any> {
try {
const result = await Storage.get({ key: key });
console.log('storageGET: ' + key + ': ' + result.value);
if (result != null) {
return result.value;
}
else {
console.log("null from service")
return null;
}
} catch (reason) {
console.log(reason);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
page1.ts
this.storageService.set('user_id',data[i].id ).then(val => console.log(val))
this.storageService.set('email', data[i].email).then(val => console.log(val))
this.storageService.set('phone_number', data[i].mobile_no).then(val => console.log(val))
this.storageService.set('firebase_uid', data[i].firebase_uid).then(val => console.log(val))
Run Code Online (Sandbox Code Playgroud)
page2.ts
this.storageService.get('user_id').then(val => console.log(val))
this.storageService.get('email').then(val => console.log(val))
this.storageService.get('phone_number').then(val => console.log(val))
this.storageService.get('firebase_uid').then(val => console.log(val))
Run Code Online (Sandbox Code Playgroud)
设置值后的console.log
检索本地存储值后的 console.log
对于每个需要它的人来说,与 Cordova 电容器不同,get API 不支持数字。因此,您必须将其转换为字符串,同时仅使用JSON.stringify(value)
例子 :
await Storage.set({
key: 'key',
value: JSON.stringify(value)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3154 次 |
| 最近记录: |