网站首页 > 教程分享 正文
除了 @Provide/@Consume 可以跨组件传递数据外。
LocalStorage 也可以实现跨组件传递数据。
首先,我们在 Index.ets 页面上创建 LocalStorage 的实例。
/**
* LocalStorage 实例
*/
const storage = new LocalStorage()
并在构造函数中初始化你要传递的数据,以 key/value 形式。
例如,{ name: "张三", age: 18 }。
/**
* LocalStorage 实例
*/
const storage = new LocalStorage({ name: "张三", age: 18 })
然后,将 storage 传给 @Entry 装饰器。
/**
* LocalStorage 实例
*/
const storage = new LocalStorage({ name: "张三", age: 18 })
@Entry(storage)
@Component
struct Index {
build() {
}
}
这一步的作用是将 LocalStorage 和页面进行绑定。
接下来,我们就使用 LocalStorage 里面的值。
使用方式:@LocalStorageProp(key)/@LocalStorageLink(key)
- LocalStorageProp 单向传递
- LocalStorageLink 双向传递
Prop 和 Link 前面几章已经详细介绍过,这里就不再赘述了。
在 Index 组件里,定义一个 @LocalStorageLink(key) 装饰的变量 name,key 为“name”,类型要和 LocalStorage 里面的数据类型一样,并且该变量必须要设置默认值。
@Entry(storage)
@Component
struct Index {
/**
* 状态变量
*/
@LocalStorageLink("name") name: string = "Index"
build() {
}
}
然后,在 build 函数里显示 name,并通过点击按钮修改 name 的值。ItemA 是一个子组件。
@Entry(storage)
@Component
struct Index {
/**
* 状态变量
*/
@LocalStorageLink("name") name: string = "Index"
build() {
Column() {
Text(this.name)
Button("Index按钮")
.onClick(() => {
this.name = "Index更新"
})
ItemA()
}
.width('100%')
}
}
ItemA 组件里使用 ItemB 子组件:
@Component
struct ItemA {
build() {
ItemB()
}
}
在 ItemB 组件里定义一个 @LocalStorageLink(key) 装饰的变量 name,key 为“name”,变量类型要和 LocalStorage 里定义的一样,变量还要有默认值:
@Component
struct ItemB {
/**
* 状态变量
*/
@LocalStorageLink("name") name: string = "ItemB"
build() {
Column() {
Text(this.name)
Button("ItemB按钮")
.onClick(() => {
this.name = "ItemB更新"
})
}
}
}
完整代码:
/**
* LocalStorage 实例
*/
const storage = new LocalStorage({ name: "张三", age: 18 })
@Entry(storage)
@Component
struct Index {
/**
* 状态变量
*/
@LocalStorageLink("name") name: string = "Index"
build() {
Column() {
Text(this.name)
Button("Index按钮")
.onClick(() => {
this.name = "Index更新"
})
ItemA()
}
.width('100%')
}
}
@Component
struct ItemA {
build() {
ItemB()
}
}
@Component
struct ItemB {
/**
* 状态变量
*/
@LocalStorageLink("name") name: string = "ItemB"
build() {
Column() {
Text(this.name)
Button("ItemB按钮")
.onClick(() => {
this.name = "ItemB更新"
})
}
}
}
运行结果:
从运行结果来看,修改 Index 组件中 name 的值,Index 和 ItemB 组件的UI刷新。
修改 ItemB 组件中 name 的值,Index 和 ItemB 组件的UI也会刷新。
总结
- 创建 LocalStorage 实例,并在构造函数完成初始化操作。
- 将 LocalStorage 与 @Entry 装饰器进行绑定。
- 使用 @LocalStorageProp/@LocalStorageLink 装饰需要同步的变量,变量必须初始化。
猜你喜欢
- 2024-09-17 两个网页,改变A页面,在B页面怎么监听到同一个localStorage变化
- 2024-09-17 Web存储Cookie,sessionStorage和localStorage
- 2024-09-17 localStorage 竟然支持存储 JavaScript 几乎所有数据类型!!!
- 2024-09-17 localStorage及sessionStorage自定义过期时间方法实现
- 2024-09-17 前端数据存储!解析SessionStorage与LocalStorage,附实例代码
- 2024-09-17 JavaScript-如何使用localStorage存储对象
- 2024-09-17 本地存储 localStorage(本地存储权限)
- 2024-09-17 说说session和cookie区别与主要应用场景,localStorage的特点
- 2024-09-17 vue3本地存储的2种方案localStorage和indexdb的优劣
- 2024-09-17 对不起 localStorage,现在我爱上 localForage了!
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)