// 父组件使用@Provide包裹数据类型,后代组件使用@Consume接收(参数名要一致),此时后代组件修改该值时,无论是复杂还是简单数据类型,父组件和后代组件使用到的这个数据都会发生变化
@Component
struct MyGrandSonComponent {
@Consume theme: ThemeObj
build() {
Column() {
Text(JSON.stringify(this.theme))
Text('MyGrandSonComponent').onClick(() => { this.theme.name = `${Math.random()}我膨胀了!!!` })
}
}
}
@Component
struct MyComponent {
@Consume theme: ThemeObj
build() {
Column() {
Text(JSON.stringify(this.theme))
MyGrandSonComponent()
}.width(300).height(100).backgroundColor(Color.Gray)
}
}
interface ThemeObj {
name: string
id: number
}
@Entry
@Component
struct Index {
@Provide theme: ThemeObj = { name: '主题', id:1 }
build() {
Column() {
Text(JSON.stringify(this.theme))
MyComponent()
}
}
}
@Provide和@Consume用法(父组件使用@Provide包裹数据类型,后代组件使用@Consume接收(参数名要一致),此时后代组件修改该值时,无论是复杂还是简单数据类型,父组件和后代组件)
最新推荐文章于 2024-09-05 10:07:24 发布