注意:
文章内名词解释:
正向:父变子也变 逆向:子变父也变
@Entry
@Component
struct Demo04 {
@State message: string = 'Hello World123'
@State obj: Aa = {
name: 'zhangsan'
}
build() {
Row() {
Column() {
Text("基本数据类型")
Son({ message: this.message })
Divider().strokeWidth(2)
Text("对象数据类型")
Son({ obj:this.obj })
Button('改变数据').onClick((event: ClickEvent) => {
this.message = '666'
})
}
.width('100%')
}
.height('100%')
}
}
@Component
struct Son {
message: string = ''
obj:Aa = {name:''}
build() {
Row() {
Text(this.message)
Text(this.obj.name)
}
}
}
class Aa{
name: string = ''
}
@Entry
@Component
struct Demo04 {
@State message: string = 'Hello World123'
@State obj: Aa = {
name: 'zhangsan'
}
build() {
Row() {
Column() {
Text(this.message)
Button('改变数据').onClick((event: ClickEvent) => {
this.message = '666'
})
.margin({
bottom:20
})
Divider().strokeWidth(5)
Text("基本数据类型")
Son({ message: this.message })
Divider().strokeWidth(2)
Text("对象数据类型")
// Son({ obj:this.obj })
}
.width('100%')
}
.height('100%')
}
}
@Component
struct Son {
@Prop message:string
// @Prop obj:Aa
build() {
Row() {
Text(this.message)
// Text(this.obj.name)
Button("逆向改变").onClick(() => {
this.message = "子变父不变"
// this.obj.name = "子变父不变"
})
}
}
}
class Aa{
name: string = ''
}
@Entry
@Component
struct Demo04 {
@State message: string = 'Hello World123'
@State obj: Aa = {
name: 'zhangsan'
}
build() {
Row() {
Column() {
Text(this.message)
Text(this.obj.name)
Button('改变数据').onClick((event: ClickEvent) => {
this.message = '666'
this.obj.name = "lisi123"
})
.margin({
bottom:20
})
Divider().strokeWidth(5)
Text("基本数据类型")
Son({message:$message})
Divider().strokeWidth(2)
Text("对象数据类型")
// Son({obj:$obj})
}
.width('100%')
}
.height('100%')
}
}
@Component
struct Son {
@Link message:string
// @Link obj:Aa
build() {
Row() {
Text(this.message)
// Text(this.obj.name)
Button("逆向改变").onClick(() => {
this.message = "子变父不变"
// this.obj.name = "子变父不变"
})
}
}
}
class Aa{
name: string = ''
}