HarmonyOS @Track使用问题
如下代码中,为什么监听不到isClick变化刷新UI
// xxx.ets
@Entry
@Component
export struct Index {
private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
@State viewModel: ViewModel = new ViewModel();
build() {
Column() {
List({ space: 20, initialIndex: 0 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('' + item)
.width('100%')
.height(100)
.fontSize(16)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
}
}, (item: string) => item)
ListItem() {
Column() {
Row() {
Column() {
Text(this.viewModel.isClick ? this.viewModel.text1 : this.viewModel.text)
.fontSize(30)
Button(this.viewModel.buttonText)
.fontSize(30)
.onClick(() => {
this.viewModel.onClick();
})
}.justifyContent(FlexAlign.Center)
}
}
}
}
.listDirection(Axis.Vertical) // 排列方向
.scrollBar(BarState.Off)
.friction(0.6)
.divider({
strokeWidth: 2,
color: 0xFFFFFF,
startMargin: 20,
endMargin: 20
}) // 每行之间的分界线
.edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
.width('90%')
}
.width('100%')
.height('100%')
.backgroundColor(0xDCDCDC)
.padding({ top: 5 })
}
}
export class ViewModel {
@Track isClick: boolean = false;
@Track text: string = "hello world!";
@Track text1: string = "hello world 999";
@Track buttonText: string = "点击";
@Track callback: Callback = {
resolve: () => {
this.isClick = true;
}
}
public onClick() {
Manager.getInstance().setCallback(this.callback);
Manager.getInstance().onClick();
}
}
export class Manager {
private static instance: Manager;
private callback?: Callback;
public setCallback(callback?: Callback) {
this.callback = callback;
}
public static getInstance() {
if (!Manager.instance) {
Manager.instance = new Manager();
}
return Manager.instance;
}
public onClick(): Promise<string> {
return new Promise((resolve) => {
setTimeout(() => {
this.callback?.resolve();
resolve("");
}, 2 * 1000);
});
}
}
export interface Callback {
resolve: () => void;
}
- 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.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
HarmonyOS
赞
收藏 0
回答 1
相关问题
HarmonyOS 关于@Track的设计
884浏览 • 1回复 待解决
HarmonyOS 新状态管理框架@observed、@track
1484浏览 • 1回复 待解决
@Track装饰器有什么作用?
1532浏览 • 1回复 待解决
HarmonyOS AudioCapturer使用问题
609浏览 • 1回复 待解决
HarmonyOS Navigation 使用问题
1177浏览 • 1回复 待解决
HarmonyOS soundpool使用问题
1160浏览 • 1回复 待解决
HarmonyOS eventHub使用问题
906浏览 • 1回复 待解决
HarmonyOS TurboModules使用问题
1154浏览 • 1回复 待解决
HarmonyOS SideBarContainer使用问题
659浏览 • 1回复 待解决
HarmonyOS RSA使用问题
477浏览 • 1回复 待解决
HarmonyOS Navigation使用问题
1266浏览 • 1回复 待解决
HarmonyOS @Link使用问题
671浏览 • 1回复 待解决
HarmonyOS resourceManager使用问题
1300浏览 • 1回复 待解决
HarmonyOS RichEditor 使用问题
1191浏览 • 1回复 待解决
HarmonyOS websocket使用问题
1047浏览 • 1回复 待解决
HarmonyOS地图使用问题
980浏览 • 1回复 待解决
HarmonyOS Worker使用问题
667浏览 • 1回复 待解决
HarmonyOS CustomDialogController使用问题
526浏览 • 1回复 待解决
HarmonyOS lottie使用问题
1049浏览 • 1回复 待解决
HarmonyOS Slide使用问题
912浏览 • 1回复 待解决
HarmonyOS ProtoBuffer使用问题
973浏览 • 1回复 待解决
HarmonyOS textpicker使用问题
727浏览 • 1回复 待解决
HarmonyOS filePreview使用问题
1034浏览 • 1回复 待解决
HarmonyOS Scroll使用问题
651浏览 • 1回复 待解决
HarmonyOS onAreaChange使用问题
1499浏览 • 1回复 待解决
@Track没有深度观测的功能,所以可能会出现数据更改但是UI未更改的情况,建议使用@ObservedV2装饰器和@Trace装饰器,详情请参考:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5
以下是参考demo: