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
赞
收藏 0
回答 1
相关问题
HarmonyOS 数组bleList,第一次更新页面正常显示,后面再次更新页面不刷新,只显示第一次的数据
795浏览 • 1回复 待解决
HarmonyOS 应用亮度获取第一次值错误,为-1
767浏览 • 1回复 待解决
HarmonyOS TextInput焦点在弹窗第一次打开时不起作用
1063浏览 • 1回复 待解决
Marquee组件的文字滚动,第一次滚动出现大量空白,如何避免空白出现
1230浏览 • 1回复 待解决
如何判断APP是否是第一次请求权限?
1134浏览 • 1回复 待解决
greenplum 表第一次insert很慢有什么办法解决?
5318浏览 • 1回复 待解决
自定义组件的传值和绑定
1658浏览 • 1回复 待解决
ArkTS自定义组件如何父子间传值?
1368浏览 • 1回复 待解决
HarmonyOS App第一次启动,需要弹出授权隐私页面吗?
1179浏览 • 1回复 待解决
如何控制web组件的pagestart和pageend仅在第一次加载url的时候触发
1031浏览 • 1回复 待解决
HarmonyOS 自定义组件必现由外部传值,如何设置?
469浏览 • 1回复 待解决
离线包 ,第一次不走缓存,第二次访问的时候怎么走webview的缓存?
1040浏览 • 1回复 待解决
HarmonyOS 第一次登录会卡输入法,会把app挤占到上方
666浏览 • 1回复 待解决
HarmonyOS 如何将一个 Builder 作为参数传个另一个 Builder?
981浏览 • 1回复 待解决
HarmonyOS 使用promptAction.openCustomDialog创建自定义弹窗builder怎么传参
1241浏览 • 1回复 待解决
HarmonyOS 自定义弹框的描述显示一个@Builder装饰的函数
852浏览 • 1回复 待解决
HarmonyOS 每次到当前页面都执行的方法,aboutToAppear只第一次调用
670浏览 • 1回复 待解决
#鸿蒙学习大百科#Tabs组件第一次点击切换时会卡顿和很慢,如何优化一下?
1240浏览 • 1回复 待解决
第一次使用HUAWEI DevEco Device Tool,提示Windows找不到文件'javaw',救救孩子
10048浏览 • 2回复 待解决
HarmonyOS 使用Tabs组件如何设置一次只加载当前显示页面的数据
781浏览 • 1回复 待解决
HarmonyOS 上架失败,第一次上架beta版应用,提交审核立刻失败了
1099浏览 • 1回复 待解决
HarmonyOS 每秒执行一次的函数
1037浏览 • 2回复 待解决
HarmonyOS 父组件通过prop传函数给子组件调用会报错
974浏览 • 1回复 待解决
@Builder自定义构建函数,如何回参?
1115浏览 • 1回复 待解决
在builder构造器中监听数据变化要使用使用@Link。