HarmonyOS 鸿蒙Next @ObservedV2+@Trace状态管理不刷新的问题
HarmonyOS 鸿蒙Next @ObservedV2+@Trace状态管理不刷新的问题
咨询描述: List中的某个Listitem代码如下,使用过程中不光需要监听数据,还需要监听数组长度的变化传递给指示器使用XFDotIndicator,指示器里监听count,@Link @Watch(‘onCountChange’) count: number,但是现在count的值一直都是0,没有变化
[@ObservedV2](/user/ObservedV2)
export class ObservedArrayWrapper<T> {
[@Trace](/user/Trace) observedArray: T[] = [];
[@Trace](/user/Trace) count: number = this.observedArray.length;
public clear(): void {
this.observedArray.splice(0, this.observedArray?.length)
}
}
@Reusable
@Component
export struct LogosItem {
logoList?: ObservedArrayWrapper<LogoData[]>;
@State model: XFDotModel = new XFDotModel()
@State index: number = 0
@State count: number | undefined = 0;
aboutToAppear(): void {
this.model.setHeight(10)
this.count = this.logoList?.count;
}
aboutToReuse(params: Record<string, Object>): void {
this.logoList = params.logoList as ObservedArrayWrapper<LogoData[]>
}
build() {
Column() {
Swiper() {
ForEach(this.logoList?.observedArray, (items: LogoData[]) => {
Grid() {
ForEach(items, (logo: LogoData) => {
GridItem() {
Column() {
ImageComponent({
imageOption: {
loadSrc: logo.iconLight,
}
})
.width('32vp')
.height('32vp')
.margin({ top: 8 })
Text(logo.name)
.fontSize('12fp')
.width('100%')
.fontSize('13fp')
.fontColor($r('app.color.text_level1'))
.textAlign(TextAlign.Center)
.margin({ top: 4 })
}
}
.width((px2vp(display.getDefaultDisplaySync().width)) / 5)
})
}
.maxCount(5)
.minCount(5)
.rowsGap(10)
.layoutDirection(GridDirection.Row)
.width('100%')
})
}
.width('100%')
.displayCount(1)
.indicator(false)
.loop(false)
.onChange((index: number) => {
this.index = index;
})
XFDotIndicator({ itemIndex: $index, count: $count, model: this.model })
.margin({ top: 4 })
}.width('100%')
.padding({ top: 4, bottom: 12 })
}
}
更多关于HarmonyOS 鸿蒙Next @ObservedV2+@Trace状态管理不刷新的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
这样的代码可以,你参考看看:
@ObservedV2
class ObData {
@Trace count: number = 0
}
@Entry
@Component
struct JSONTest {
obData: ObData = new ObData()
build() {
Column({ space: 20 }) {
CompAAA({
count: this.obData.count
})
Text(${this.obData.count}).onClick(() => {
this.obData.count++
})
.width("100%")
.backgroundColor(Color.Red)
}
.width(200)
.height(500)
}
}
@Component
export struct CompAAA {
@Prop @Watch('onCountChange') count: number
onCountChange() {
console.log("")
}
build() {
Text(${this.count}).width("100%").backgroundColor(Color.Green)
}
}
更多关于HarmonyOS 鸿蒙Next @ObservedV2+@Trace状态管理不刷新的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html