HarmonyOS @Builder函数接收的状态变量未引起内部ui变化

ChangePinPage界面唤起了自定义键盘,在自定义键盘的按钮组件中修改了currentKeyboardType键盘类型来切换中英文键盘,但是CustomKeyboard.ets中Text(${$$.currentKeyboardType})的值并未变化。

//xxxModel.ets
export class Tmp {
  inputController: TextInputController;
  currentKeyboardType: number;
  constructor(inputController: TextInputController, currentKeyboardType: number) {
    this.inputController = inputController;
    this.currentKeyboardType = currentKeyboardType
  }
}
# ChangePinPage.ets:
@Provide currentKeyboardType: number = Const.KEYBOARD_TYPE_NUMBER; 
...
TextInput()
.customKeyboard(KeyBoardWindow(new Tmp(this.oldPinController, this.currentKeyboardType)))
# CustomKeyboard.ets:
@Builder
export function KeyBoardWindow($$: Tmp) {
  Column() {
    Row() {
      Image($r("app.media.ic_keyboard_down"))// 标题行的”收起“图标
        .onClick(() => {
          $$.inputController.stopEditing()
        })
      Text(`${$$.currentKeyboardType}`)
   }
}
// xxxChild.ets ...
@Consume currentKeyboardType: number;
...
this.currentKeyboardType = 2;
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

全局@Builder自定义构建函数,采用引用传递,如果放在Navigation()的menus内,状态变量的改变不会触发@Builder方法内的UI刷新

按引用传递参数时,传递的参数可为状态变量,且状态变量的改变会引起@Builder方法内的UI刷新。参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builder-V5

相关demo

@Builder function ABuilder($$: { paramA1: string }) {
Row() {
Text(`UseStateVarByReference: ${$$.paramA1} `)
}
}
@Entry
@Component
struct Parent {
@State label: string = 'Hello';
build() {
Column() {
// 在Parent组件中调用ABuilder的时候,将this.label引用传递给ABuilder
ABuilder({ paramA1: this.label })
Button('Click me').onClick(() => {
// 点击“Click me”后,UI从“Hello”刷新为“ArkUI”
this.label = 'ArkUI';
})
}
}
}
分享
微博
QQ
微信
回复
2天前
相关问题
ArkTS中如何监听状态变量变化
1005浏览 • 1回复 待解决
状态变量和常规变量有什么区别?
463浏览 • 2回复 待解决
HarmonyOS 状态变量不刷新问题
494浏览 • 1回复 待解决
关于状态变量@state必须知道
1153浏览 • 1回复 待解决