@Entry
@Component
struct ProvideCase {
@State message: string = 'Hello World'
@Provide money: number = 10000
build() {
Row() {
Column() {
Text(`root组件money:${this.money}`).fontSize(30)
Divider().strokeWidth(6).color(Color.Green)
Father()
}
.width('100%')
}
.height('100%')
}
}
@Component
struct Father {
@Consume money: number
build() {
Column() {
Text(`father组件money:${this.money}`).fontSize(30)
.onClick(() => {
this.money += 10
})
Divider().strokeWidth(6).color(Color.Red)
Son()
}
}
}
@Component
struct Son {
@Consume money: number
build() {
Text(`Son组件money:${this.money}`).fontSize(30)
.onClick(() => {
this.money = this.money - 10
})
}
}
image.png
网友评论