Typescript 如何正确设置和获取使用本机存储的ionic 4?

Typescript 如何正确设置和获取使用本机存储的ionic 4?,typescript,angular7,ionic4,Typescript,Angular7,Ionic4,我正在创建Ionic 4 plus Angular应用程序,并使用本机存储 在本地保存应用程序数据。下面的代码我正在使用,但当我调用get value时,每次都会给null this.storage.set('myValue',this.value); this.storage.get('myValue')。然后(value=>{ log('我的值:'+值); })主要思想是,我们将存储一个包含我们使用的所有密钥的数组,这样我们就可以在其中循环并删除每个密钥的数据 让我们假设以下示例: fir

我正在创建Ionic 4 plus Angular应用程序,并使用本机存储 在本地保存应用程序数据。下面的代码我正在使用,但当我调用get value时,每次都会给null

this.storage.set('myValue',this.value);
this.storage.get('myValue')。然后(value=>{
log('我的值:'+值);

})
主要思想是,我们将存储一个包含我们使用的所有密钥的数组,这样我们就可以在其中循环并删除每个密钥的数据

让我们假设以下示例:

firstService

export clas FirstService {
   constructor(garbage: GarbageCollecterService){}

  // Let's assume here you are storing data in memroy
  storeDataInMemory(key1) {
    ...
    // At the end of the function store the key in an array , We need it later
    this.garbage.storeAllKeys(key);
   }
}
export clas SecondService {
   constructor(garbage: GarbageCollecterService){}
  storeDataInMemory(key2) {
    ...
    // At the end of the function store the key in an array , We need it later
    this.garbage.storeAllKeys(key);
   }
}
secondService

export clas FirstService {
   constructor(garbage: GarbageCollecterService){}

  // Let's assume here you are storing data in memroy
  storeDataInMemory(key1) {
    ...
    // At the end of the function store the key in an array , We need it later
    this.garbage.storeAllKeys(key);
   }
}
export clas SecondService {
   constructor(garbage: GarbageCollecterService){}
  storeDataInMemory(key2) {
    ...
    // At the end of the function store the key in an array , We need it later
    this.garbage.storeAllKeys(key);
   }
}
现在,您只需调用
clearData()


这里是文档。

我们需要记住get和set是异步调用。我们需要在set完成后进行get。在set的回调中,我们需要调用get,这就是为什么我们会得到过时的值。

Hi@gaus在尝试与它交互之前,请确保您正在等待storage.ready()。诸如此类

 this.storage.ready().then(() => {
      return this.storage.get('myValue');
    });

或者您可以使用本地存储而不是

您的意思是要清除浏览器的现金?每当您使用一个键设置一个值时,将所有键存储在一个数组中,然后循环遍历该数组,并为每个元素调用存储。删除(键),这样您就不会对所有服务使用相同的存储对象,我的意思是对所有应用程序,创建一个不同的服务,该服务只负责处理从同一对象获取、设置和删除数据。您是在使用同一个存储对象,还是在为每个服务或页面创建一个新的存储对象?也许您不明白,请在所有页面上随意创建一个存储对象