harmony学习AppStorage在多个UIAbility组件之间的应用

2024-03-18 17:46 harmony学习AppStorage在多个UIAbility组件之间的应用已关闭评论

需求

在多个UIAbilit组件之间数据共享。在ERP管理中,app登录界面提供了默认的测试账套、账号、密码,方便用户体验app的功能。

当前鸿蒙开发的app中,登录界面预设了账套、账号、密码、组织等信息。

AppStorage

AppStorage应用全局的UI状态存储,登录主界面预设了默认的测试账套、组织、账号、密码等信息,学习AppStorage基本使用

创建属性值

//应用全局的默认值,不存在key则创建
AppStorage.SetOrCreate('dz_tenant','zhbm001')
AppStorage.SetOrCreate('dz_org','001')
AppStorage.SetOrCreate('dz_account','admin')
AppStorage.SetOrCreate('dz_psw','admin')

获取属性值:Get()方法

let tenant=AppStorage.Get('dz_tenant')
let org=AppStorage.Get('dz_org')
let account=AppStorage.Get('dz_account')
let psw=AppStorage.Get('dz_psw')

设置属性值

//获取属性值code by每日教程teachcourse.cn
var dz_tenant:SubscribedAbstractProperty<string>=AppStorage.Link('dz_tenant')
var dz_org:SubscribedAbstractProperty<string>=AppStorage.Link('dz_org')
var dz_account:SubscribedAbstractProperty<string>=AppStorage.Link('dz_account')
var dz_psw:SubscribedAbstractProperty<string>=AppStorage.Link('dz_psw')

//设置新的属性值
dz_tenant.set('zhbm002')
dz_org.set('002')
dz_account.set('小黄')
dz_psw.set('admin')

完整的用法

//应用全局的默认值
//code by每日教程teachcourse.cn
AppStorage.SetOrCreate('dz_tenant', 'zhbm001')
AppStorage.SetOrCreate('dz_org', '001')
AppStorage.SetOrCreate('dz_account', 'admin')
AppStorage.SetOrCreate('dz_psw', 'admin')
//获取属性值
var dz_tenant: SubscribedAbstractProperty<string> = AppStorage.Link('dz_tenant')
var dz_org: SubscribedAbstractProperty<string> = AppStorage.Link('dz_org')
var dz_account: SubscribedAbstractProperty<string> = AppStorage.Link('dz_account')
var dz_psw: SubscribedAbstractProperty<string> = AppStorage.Link('dz_psw')

@Entry
@Component
struct DzIndex {
  //获取属性值Get()方法
  dz_account: string = AppStorage.Get('dz_account')
  dz_psw: string = AppStorage.Get('dz_psw')

  build() {
    Column() {
      DzEditText({ des: '用户名', inputHint: '请输入用户名', content: this.dz_account })
      DzEditText({ des: '密码', inputHint: '请输入密码', content: this.dz_psw })

    }.width('100%')
    .height('100%')
  }

}

当前文章价值1.3元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://www.teachcourse.cn/3151.html ,谢谢支持!

资源分享

分类:harmony 标签:,
mysql重新启动失败 mysql重新启动失败
浅谈GC机制 浅谈GC机制
python库tkinter实现选择多文件上传 python库tkinter实现选择多文件上
Python单例模式封装pymysql,包括数据库创建、表创建和增删改查方法 Python单例模式封装pymysql,包

评论已关闭!