我是Ionic的新手.
我能够使用IONIC Storage Service存储A JSON对象(数据); 但是,当我尝试检索JSON数据时,我得到了未定义.
我感谢任何帮助 - 下面是我的代码:
Provider:storage-service.ts:我能够存储和输出数据,但是我无法返回数据:
import {Injectable} from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class StorageService {
constructor(public storage: Storage){}
public storeData (data) {
this.storage.set('data', data);
}
public getStoredData() {
this.storage.get('data').then((val) => {
console.dir(val); //****** this outputs the object.
return val; //***** this returns undefined
});
}
}
Run Code Online (Sandbox Code Playgroud)
我-page.ts:
import {StorageService} from "../../providers/storage-service";
constructor(public storageService: StorageService) {
//***** this outputs undefined, what I'm doing wrong?
console.dir(this.storageService.getStoredData());
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助.
谢谢