需求
在多个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元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)
你可能感兴趣的文章
分类:harmony
标签:AppStorage, harmony
评论已关闭!