HarmonyOS 鸿蒙Next:对象用ComponentV2+@Trace修饰,值变了但是ui未更新

发布于 1周前 作者 yibo5220 来自 鸿蒙OS

HarmonyOS 鸿蒙Next:对象用ComponentV2+@Trace修饰,值变了但是ui未更新

请问下,为什么我的TagItemComponent行布局中的TagModel对象里面的isCheck字段已经用ComponentV2+@Trace修饰了,但是当我点击行布局修改isCheck和tagName后,值变了,但是ui都没有更新,能有官方帮忙看下原因吗?

@Component
export struct TabTodoComponent {
//头部沉浸式状态栏的高度
@StorageProp(Constant.SYSTEM_TOP_HEIGHT) systemTopHeight: number = 0
tagList :Array<TagModel> = []

aboutToAppear(): void {
this.tagList = TagList
}

build() {
Scroll() {
Column() {
List({ space: 10 }) {
ForEach(this.tagList, (item: TagModel, index: number) => {
ListItem() {
TagItemComponent({ item: item })
}
}, (item: string, index: number) => JSON.stringify(item) + index)
}
.width(‘100%’)
.margin({ top: 10 })
.lanes(4, 10)
}
.padding({ left: 15, right: 15 })
.alignItems(HorizontalAlign.Start)
.width(‘100%’)
}
.align(Alignment.Top)
.width(‘100%’)
.height(‘100%’)
.backgroundColor($r(‘app.color.default_background_color’))
}

}

@ComponentV2
export struct TagItemComponent {
@Require @Param item: TagModel

build() {
Stack(){
Column({ space: 8 }) {
Image(this.item.tagIcon)
.width(30)
.height(30)

Text(this.item.tagName)
.fontSize(14)
.fontColor($r(‘app.color.color_666666’))
}
.justifyContent(FlexAlign.Center)
.width(‘100%’)
.height(‘100%’)

Image($r(‘app.media.icon_rightselect’))
.visibility(this.item.isCheck ? Visibility.Visible : Visibility.None)
.width(13)
.height(13)
}
.align(Alignment.BottomEnd)
.width(‘100%’)
.height(80)
.backgroundColor(Color.White)
.borderRadius(10)
.onClick(()=>{
this.item.tagName = ‘123’
this.item.isCheck = !this.item.isCheck
showToast(${this.item.isCheck})
})
}
}

export const TagList: Array<TagModel> = [
{
tagIcon: $r(‘app.media.tag_gongzuo’),
tagName: ‘工作’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_shenghuo’),
tagName: ‘生活’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_lvyou’),
tagName: ‘旅游’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_richang’),
tagName: ‘日常’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_xuexi’),
tagName: ‘学习’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_zuoye’),
tagName: ‘作业’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_yule’),
tagName: ‘娱乐’,
isCheck: false
},
{
tagIcon: $r(‘app.media.tag_qita’),
tagName: ‘其他’,
isCheck: false
}
]


关于HarmonyOS 鸿蒙Next:对象用ComponentV2+@Trace修饰,值变了但是ui未更新的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

5 回复

希望HarmonyOS能加强与其他品牌设备的兼容性,让更多人受益。

为什么用[@ObservedV2](/user/ObservedV2)包[@Trace](/user/Trace) 传值是undefine  闪退 只有视图更新是正常的

@Trace需要和@ObservedV2搭配使用,另外CompomentV1和ComponentV2不能混合使用

有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html

子 对象 需要跟UI绑定的字段也需要加 Trace
[@Trace](/user/Trace) isCheck
回到顶部