HarmonyOS 自定义弹窗组件,builder 函数将组件作为值,传进去,第一次显示,@prop 数据不更新

在app中实现一个Toast组件功能,使用自定义弹窗组件进行二次封装,关键代码如下:

struct CustomerDialogToast{
  controller?: CustomDialogController;
  @Prop type:string = "info"
  @Prop info:string = ""
  build() {
    Column(){
      if(this.type=="info"){
        Text(this.info)
          .fontSize(CommonConstants.fontSize)
          .fontColor(CommonConstants.fontColorInfo).maxLines(1)
      }
      if (this.type=="loading"){

      }
    }
    .width(CommonConstants.toastWidth)
    .borderRadius(CommonConstants.borderRadius)
    .borderColor(CommonConstants.borderColor)
    .borderWidth(CommonConstants.borderWidth)
    .backgroundColor(CommonConstants.backgroundColor)
    .padding({
      top:CommonConstants.paddingTop,bottom:CommonConstants.paddingTop,
      left:CommonConstants.paddingLeft,right:CommonConstants.paddingLeft
    })
  }
}

@Component
export struct CustomDialogUser{
  @Prop type:string = "info"
  @Prop info:string = ""

  @Link @Watch('onShowUpdate') showToast:boolean;

  dialogController:CustomDialogController |null = new CustomDialogController({
    builder:CustomerDialogToast({type:this.type,info:this.info}),
    openAnimation: {
      duration: CommonConstants.DURATION,
      curve: Curve.Friction,
      delay: CommonConstants.DELAY,
      playMode: PlayMode.Alternate
    },
    closeAnimation: {
      duration: CommonConstants.DURATION,
      curve: Curve.Friction,
      delay: CommonConstants.DELAY,
      playMode: PlayMode.Alternate
    },
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy:  CommonConstants.offsetY},
    customStyle: true,
    cornerRadius:CommonConstants.cornerRadius,
    isModal: false
  })

  onShowUpdate(value:boolean){
    console.log("点击",this.showToast)
    this.info = this.info
    if(this.showToast){
      this.dialogController?.open()
      setTimeout(()=>{
        this.dialogController?.close()
        this.showToast = false
      },3000)
    }else{
      this.dialogController?.close()
      this.showToast = false
    }
  }
  aboutToDisappear(){
    this.dialogController = null
  }
  build() {
  }
}
  • 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.

然后在其他组件引入CustomDialogUser。

build() {
  Column(){
    CustomDialogUser({ info: this.showInfo, type: "info",showToast:this.showToast })
  }
  • 1.
  • 2.
  • 3.
  • 4.

在处理逻辑上

if(!this.isValidPhone){
  this.showInfo = "请输入正确手机号 !"
  this.showToast = true
  return
}
if(!this.isChecked){
  this.showInfo = "请阅读并同意协议 !"
  this.showToast = true
  return
}
if(!this.isCoded){
  this.showInfo = "请输入验证码 !"
  this.showToast = true
  return
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

第一次弹窗内容为空 ,直到第二次点击才显示 “请输入正确手机号”的提示,如何解决第一次文字提醒为空?

HarmonyOS
2024-12-24 16:58:36
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

在builder构造器中监听数据变化要使用使用@Link。

分享
微博
QQ
微信
回复
2024-12-24 19:40:44
相关问题
如何判断APP是否是第一次请求权限?
1134浏览 • 1回复 待解决
自定义组件和绑定
1658浏览 • 1回复 待解决
ArkTS自定义组件如何父子间
1368浏览 • 1回复 待解决
HarmonyOS 每秒执行一次函数
1037浏览 • 2回复 待解决
@Builder自定义构建函数,如何回参?
1115浏览 • 1回复 待解决