PersistentStorage持久化第二次获取不到,在两个model里

在entry中使用PersistentStorage仅从存储,在httpLibrary中不能获取不到存储的数据,在entry中可以获取,但是第一次是可以获取的,第二次就获取不到了。

1 在entry中进行网络请求,获取用户 token

2 使用PersistentStorage存储token

3 从PersistentStorage获取token进行请求别的接口

4 从后台退出程序并且启动

5 从PersistentStorage获取token进行请求别的接口

6 获取不到token

HarmonyOS
2024-08-02 18:06:05
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
xiaohur
PersistentStorage.persistProp('aProp', 20) 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World' 
  @State aProp: number|undefined = 48 
  //@StorageProp('aProp') aPropD: number = 0; 
 
  build() { 
    Row() { 
      Column() { 
        //Text(this.aPropD.toString()) 
        Text(this.message) 
        // 应用退出时会保存当前结果。重新启动后,会显示上一次的保存结果 
        Text(`${this.aProp}`) 
          .onClick(() => { 
            if(this.aProp){ 
              this.aProp++; 
              PersistentStorage.persistProp('aProp', this.aProp); 
              AppStorage.setOrCreate("aProp",this.aProp) 
            } 
            //this.aProp += 1; 
          }) 
      } 
    } 
  } 
 
  aboutToAppear(): void { 
    this.aProp = AppStorage.get("aProp"); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

参考上面demo验证,是可以进行数据持久化的(后台杀掉,重新进入,数据可持久化)。

分享
微博
QQ
微信
回复
2024-08-02 22:07:10
相关问题
PersistentStorage如何持久对象?
3701浏览 • 1回复 待解决
HarmonyOS aioxs二次封装
993浏览 • 1回复 待解决
HarmonyOS 权限二次申请
841浏览 • 1回复 待解决
PersistentStorage持久存储问题
1672浏览 • 0回复 待解决
class二次刷新渲染数组
1549浏览 • 1回复 待解决
HarmonyOS getStringByName方法二次封装
992浏览 • 1回复 待解决
实现二次侧滑退出应用
2779浏览 • 1回复 待解决
HarmonyOS cocos引擎能否二次启动
1209浏览 • 1回复 待解决
华为账号实时验证/二次放号相关咨询
2865浏览 • 1回复 待解决