【HarmonyOS 鸿蒙 黑马程序员 学习笔记 116.1】
·
@Link 双向同步
- 子建立Link 变量
- 父绑定子
注意 Link 建立变量的时候 不能赋值
@Provide 和 @Consume 无视层级传递

- @Provide 为父 没有绑定步骤 创建及绑定
- @Consume 为子 子变量名必须与父变量名保持一致 创建及绑定
@Observed 和 @ObjectLink 联动

ObjectLink并不成功IDE一直报错
并未找到问题原因,官方也没查询到问题原因
The '@ObjectLink' decorated attribute 'itme' must be an '@Observed' decorated class or a union of '@Observed' decorated class and undefined or null, or both. <ArkTSCheck>
简易示例
ObjectLink并不成功IDE一直报错
@Component struct ui_4{
@Link str4 : string
@Consume stra : string // 变量名必须与父层保持一致 创建及绑定
build() {
Column(){
Text(this.str4).fontSize(30)
Text(this.stra).fontSize(30)
Button('str 4')
.onClick(()=>{
this.str4 == '4层'? this.str4='四层':this.str4='4层'
this.stra == '无视'? this.stra='层级':this.stra='无视'
})
}
}
}
@Component struct ui_3{
@Link str3 : string
build() {
Column(){
Text(this.str3).fontSize(30)
Button('str 3')
.onClick(()=>{this.str3 == '3层'? this.str3='三层':this.str3='3层'})
ui_4({str4:this.str3})
}
}
}
@Component struct ui_2{
//@Link str2 : string = '二层' // 无需初始化的意思不用赋值
@Link str2 : string
build() {
Column(){
Text(this.str2).fontSize(30)
Button('str 2')
.onClick(()=>{
this.str2 == '2层'? this.str2='二层' : this.str2='2层'
})
ui_3({str3:this.str2})
}
}
}
interface dx {name:string,age:number}
//let sz1 : dx[] = [{name:'小明',age:12},{name:'小芳',age:11},{name:'小明',age:13}]
@Observed class lei implements dx {
name:string
age:number
constructor(c:dx) {
this.name = c.name
this.age = c.age
}
}
@Component struct ui_ob2{
@Prop itme : dx // ObjectLink并不成功IDE一直报错
build() {
Column(){
Text(`${this.itme.age}`)
Button('对象测试 2').onClick(()=>{this.itme.age++})
}
}
}
@Component struct ui_ob1{
@State leix : lei[] = [
new lei({name:'小明',age:12}),
new lei({name:'小芳',age:11}),
]
build() {
Column(){
Text(`${this.leix[0].age}`)
Button('对象测试 1').onClick(()=>{this.leix[0].age++})
ui_ob2({itme:this.leix[0]})
}
}
}
@Entry
@Component struct Index {
@State str1 : string = '顶层'
@Provide stra : string = '穿透' // 没有绑定步骤 创建及绑定
build() {
Column(){
Text(this.str1).fontSize(30) // 展示
Text(this.stra).fontSize(30)
Button('str 1') // 修改
.onClick(()=>{
this.str1 == '顶层'? this.str1='一层':this.str1='顶层'
this.stra == '穿透' ? this.stra = '多层' : this.stra = '穿透'
})
ui_2({str2:this.str1}) // 绑定
ui_ob1()
}.width('100%').height('100%')
}
}

更多推荐
所有评论(0)