离子 4 电容器存储无法正常工作

ionic 4 capacitor storage get not working properly

最近我从 Cordova 转向了电容器存储 API。我有一个非常奇怪的问题。在第 1 页,我将 4 个值设置为本地存储,但是当我在另一页上访问这些值时,一个值显示为 null。

我尝试了很多方法,但值 user_id 始终为空。

我确保值存储在本地存储中。

我还附上 console.log 的图像,在设置和从本地存储中检索值后

storage.service.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;
  }
  }

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))

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))

Console.log 设置值后

console.log 检索本地存储值后

对于需要它的每个人,与 Cordova 电容器不同,get API 不支持数字。 所以你必须把它转换成字符串,同时只使用 JSON.stringify(value)

设置到存储中

示例:

await Storage.set({
      key: 'key',
      value: JSON.stringify(value)
    });