HarmonyOS 关于数组包裹对象中更新组件问题
以下代码中Info对象中real_name字段期望点击griditem的时候更新reanl_name信息这个如何实现啊,目前是数组更新了,但是real_name没及时更新。
import { ObservedArray } from '@ohos/common'
@Preview
@Component
export struct GridTest {
@State beanList: ObservedArray<Info> = []
aboutToAppear(): void {
this.beanList.push(new Info("1"))
this.beanList.push(new Info("2"))
this.beanList.push(new Info("3"))
this.beanList.push(new Info("4"))
this.beanList.push(new Info("5"))
this.beanList.push(new Info("6"))
this.beanList.push(new Info("7"))
}
build() {
Column() {
Grid() {
ForEach(this.beanList, (item: Info, index) => {
GridItem() {
Column() {
Text(item.real_name)
.fontColor($r('app.color.scaffold_fg_dark_2'))
.fontSize(12)
.padding({ top: 5, bottom: 2 })
.maxLines(1)
.textAlign(TextAlign.Center)
.visibility(Visibility.Visible)
}
}.borderRadius(8)
.size({width:80,height:80})
.backgroundColor(Color.Red)
.borderWidth(0).onClick(() => {
this.beanList[index].real_name = index + "SS"
console.log("-----onClick-grid---" + this.beanList[index].real_name + ",," + item.real_name)
})
}, (item: Info) => JSON.stringify(item))
}
.rowsGap(10)
.columnsGap(16)
.padding({
bottom: 12,
top: 10
})
}.width("100%").backgroundColor($r('app.color.color_ffffff'))
}
}
@Observed
export class Info {
constructor(real_name: string) {
this.real_name = real_name
}
real_name?: string = '';
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
HarmonyOS
赞
收藏 0
回答 1
相关问题
HarmonyOS 关于数组中class更改,List不触发更新问题
675浏览 • 1回复 待解决
HarmonyOS 对象数组在使用@Observed和@ObjectLink驱动子组件UI更新时的问题
751浏览 • 1回复 待解决
HarmonyOS 对象内数组排序问题
548浏览 • 1回复 待解决
HarmonyOS 数组中对象的排序
683浏览 • 1回复 待解决
HarmonyOS @Observed装饰的类,构造了对象数组,那这个数组中对象的属性变化之后,能引起视图的更新吗
1612浏览 • 1回复 待解决
HarmonyOS 对象内数组内对象属性的修改问题
444浏览 • 1回复 待解决
HarmonyOS 关于WrapperBuilder更新问题
375浏览 • 1回复 待解决
HarmonyOS 对象A数组,对象A中又嵌套一个对象B数组,修改B中的属性UI,不刷新
1076浏览 • 1回复 待解决
HarmonyOS 如何实现根据数组元素对象属性的变化同步更新UI
603浏览 • 1回复 待解决
HarmonyOS 关于对象数组内容变更不会引起UI刷新
1143浏览 • 0回复 待解决
HarmonyOS 关于objectLink在数组对象中使用时,修改数据页面不刷新问题
1241浏览 • 1回复 待解决
HarmonyOS viewModel中获取数组数据,怎么更新ui
290浏览 • 1回复 待解决
关于发布证书更新的问题
791浏览 • 1回复 待解决
HarmonyOS 关于APP版本升级更新的问题
368浏览 • 1回复 待解决
HarmonyOS关于JSVM中创建原生对象的生命周期问题
674浏览 • 1回复 待解决
HarmonyOS 数组对象数据刷新
546浏览 • 1回复 待解决
如何判断一个对象是否在对象数组中
2952浏览 • 1回复 待解决
HarmonyOS Navigation包裹Splash界面显示问题
474浏览 • 1回复 待解决
#鸿蒙通关秘籍#如何在HarmonyOS中通过BaseNavigation包裹子组件
441浏览 • 1回复 待解决
HarmonyOS @State可以修饰对象数组
425浏览 • 1回复 待解决
HarmonyOS 使用tab组件,列表对象如何动态更新内容。
811浏览 • 1回复 待解决
HarmonyOS Scroll包裹的控件默认居中问题
852浏览 • 1回复 待解决
HarmonyOS 数组子属性修改怎么更新显示
457浏览 • 1回复 待解决
HarmonyOS @state可以修饰对象数组么
391浏览 • 1回复 待解决
HarmonyOS 分组列表数组嵌套时数据更新不触发列表更新
517浏览 • 1回复 待解决
请参考下方代码:
每个属性都要实例化,通过json解析,ArkUI无法监听对象中数组属性的push变化,json解析的为非class对象,class内的属性变化不能被观察到。