HarmonyOS 数组中嵌套对象的属性值发生变化,@Watch监听不到数组的变化,这个要怎么办
代码如下:
@Observed
class ProductModel{
name:string = ''
count:number = 0
}
@Entry
@Component
struct ProductListView {
showTitle:boolean = false;
jsonString:string = '[{"name":"商品1", "count":"1"},{"name":"商品2", "count":"1"},{"name":"商品3", "count":"1"}]'
@State @Watch('productListChanged') productList: ProductModel[] = [];
productListChanged(){
this.showTitle = !this.showTitle;
}
aboutToAppear(): void {
this.productList = JSON.parse(this.jsonString);
let list: ProductModel[] = [];
for (const element of this.productList) {
let product: ProductModel = new ProductModel();
product.name = element.name;
product.count = element.count;
list.push(product);
}
this.productList = list;
}
build() {
Column({space:10}){
if(this.showTitle){
Text('标题')
}
ForEach(this.productList,(item:ProductModel)=>{
Column(){
Text('这里无法刷新__'+`${item.count}`)//此处无法刷新,该怎么解决?
ProductView({item:item, countChanged:(product)=>{
//productList数组中嵌套对象item的属性值发生变化,监听不到数组的变化,无法触发productListChanged()方法
}})
}
})
}.padding(20)
}
}
@Component
struct ProductView {
@ObjectLink item: ProductModel;
countChanged: (item:ProductModel)=>void = ()=>{};
build() {
Row(){
Text(this.item.name)
Text(`${this.item.count}`)
Button('数量-1').onClick(()=>{
this.item.count--
if (this.countChanged !== undefined) {
this.countChanged(this.item);
}
})
Button('数量+1').onClick(()=>{
this.item.count++
if (this.countChanged !== undefined) {
this.countChanged(this.item);
}
})
}.width('100%').justifyContent(FlexAlign.SpaceBetween)
}
}
- 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.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
数组中嵌套对象的属性值发生变化,@Watch监听不到数组的变化,这个要怎么办?
HarmonyOS
赞
收藏 0
回答 1
相关问题
HarmonyOS 使用ObjectLink观察对象中的数组,修改数组长度,监听不到数据变化
1444浏览 • 1回复 待解决
HarmonyOS DataSource数据发生变化,Tabs循环的tabContent未发生变化
841浏览 • 1回复 待解决
如何监听数组内对象属性变化
3173浏览 • 1回复 待解决
HarmonyOS 页面高度发生变化
941浏览 • 1回复 待解决
HarmonyOS 如何监听数组对象中元素属性的变化
1256浏览 • 1回复 待解决
HarmonyOS 如何监听数组元素属性的变化?
1618浏览 • 1回复 待解决
HarmonyOS @Watch装饰器,能监听对象中某个属性的变化值吗?
1037浏览 • 1回复 待解决
HarmonyOS @Observed装饰的类,构造了对象数组,那这个数组中对象的属性变化之后,能引起视图的更新吗
1902浏览 • 1回复 待解决
网站.net改PHP,链接发生变化怎么设置301跳转?
2691浏览 • 0回复 待解决
HarmonyOS 基础组件渲染的内容发生变化 添加fade动画
666浏览 • 1回复 待解决
HarmonyOS @Watch 如何检测数组内元素变化
1126浏览 • 1回复 待解决
后续异常信息是否会根据版本的迭代发生变化?
1259浏览 • 1回复 待解决
HarmonyOS Canvas的在onReady函数里link对象发生变化并不会重新走判断
604浏览 • 1回复 待解决
HarmonyOS 如何实现根据数组元素对象属性的变化同步更新UI
1032浏览 • 1回复 待解决
HarmonyOS 容器组件子组件个数发生变化时,有什么监听回调方法吗
733浏览 • 1回复 待解决
使用@LocalStorageProp修饰的变量,当子组件中的变量值变了,@Entry页面中的值没有发生变化。
1193浏览 • 1回复 待解决
HarmonyOS @State修饰的数组内部的值变化了,怎么刷新UI?
1512浏览 • 1回复 待解决
BundleInfo.appInfo.accessTokenId使用过程中是否会发生变化
2830浏览 • 1回复 待解决
web组件嵌套滚动在折叠屏展开后收起时其滚动位置会发生变化
856浏览 • 1回复 待解决
HarmonyOS 对象A数组,对象A中又嵌套一个对象B数组,修改B中的属性UI,不刷新
1464浏览 • 1回复 待解决
HarmonyOS ArkTS堆栈格式结构?后续ArkTS堆栈结构是否会发生变化?
709浏览 • 1回复 待解决
HarmonyOS 对象数组在@state修饰时,对象属性变化界面不刷新的解决办法
1002浏览 • 1回复 待解决
HarmonyOS WaterFlow数据更新后,使用scrollToIndex(0)后,瀑布流数据发生变化
805浏览 • 1回复 待解决
#鸿蒙学习大百科#如何实现父组件尺寸发生变化,但是子组件的宽高比不变化?
931浏览 • 0回复 待解决
HarmonyOS 用@Watch来订阅数据变化时候,数据中每个属性变化都会导致调用这个@Watch方法
1143浏览 • 2回复 待解决
参考示例: