弹窗组件调用父组件函数传递

开发中,经常有那种添加功能的弹窗,希望在弹框里编辑添加的内容,然后完成显示到主页里,这时候就通过把父组件里的添加方法传递给子组件,子组件调用方法并传递编辑的信息。开发者可以通过CustomDialogController类显示自定义弹窗,实现应用的业务场景

HarmonyOS
2024-05-26 12:35:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
savage01

使用的核心API

自定义弹窗

核心代码解释

关键就是把saveTarget方法传递给弹窗,弹窗在通过调用confirm,这里的需要用到confirm:()=> this.onAccept()箭头函数的方式传递,普通方式会出现this指向的问题。

//主界面 
@Entry 
@Component 
struct Index { 
  @State targetList:ITarget[] = [] 
  
  build() { 
    Column() { 
      Text('工作目标').fontSize(30).fontWeight(600).width(Constance.fullSize).margin({bottom:20}) 
      SubGoal({targetList:$targetList}) 
      AddTargetDialog({onClickOk:(value:string):void=>this.saveTarget(value)}).position({x:110,y:700}) 
  
    } 
    .width(Constance.fullSize) 
    .height(Constance.fullSize).backgroundColor('#eee').padding(10) 
  } 
  
  saveTarget(value){ 
    console.log('saveTarget',value) 
    let time = new Date() 
    this.targetList.push({ 
      process:0, 
      title:value, 
      time 
    }) 
    console.log('targetList',JSON.stringify(this.targetList)) 
  } 
} 
  
//弹框组件 
@Preview 
@Component 
export default struct AddTargetDialog { 
  @State textValue: string = '' 
  @State inputValue: string = '' 
  onClickOk?:(value:string)=>void 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: CustomDialogExample({ 
      // cancel: this.onCancel,  //这里不能用普通赋值,会调用失败 
      cancel:()=> this.onCancel(), 
      // confirm: this.onAccept,  //这里不能用普通赋值,会调用失败 
      confirm:()=> this.onAccept(), 
      textValue: $textValue, 
      inputValue: $inputValue 
    }), 
    cancel: this.existApp, 
    autoCancel: true, 
    alignment: DialogAlignment.Bottom, 
    offset: { dx: 0, dy: -20 }, 
    gridCount: 4, 
    customStyle: false 
  }) 
  // 在自定义组件即将析构销毁时将dialogControlle删除和置空 
  aboutToDisappear() { 
    delete this.dialogController, // 删除dialogController 
    this.dialogController = undefined // 将dialogController置空 
    console.log('aboutToDisappear') 
  } 
  aboutToAppear(){ 
    console.log('appear',typeof  this.onClickOk) 
    this.onClickOk('测试') 
  } 
  
  onCancel() { 
    console.info('Callback when the first button is clicked') 
    console.log('onCancel',JSON.stringify(this),typeof this.onClickOk) 
  } 
  
  onAccept() { 
    console.info('Callback when the second button is clicked',this.inputValue) 
    console.log('onclick',JSON.stringify(this),typeof this.onClickOk) 
    if(this.onClickOk!==undefined){ 
      this.onClickOk(this.inputValue) 
      console.log('执行') 
    } 
  } 
  
  existApp() { 
    console.info('Click the callback in the blank area') 
  } 
  
  build() { 
    Column() { 
      Button('添加子目标') 
        .onClick(() => { 
          if (this.dialogController != undefined) { 
            this.dialogController.open() 
          } 
        }).backgroundColor(0x317aff) 
    } 
  } 
} 
  
@CustomDialog 
struct CustomDialogExample { 
  @Link textValue: string 
  @Link inputValue: string 
  controller: CustomDialogController 
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在最后 
  cancel: () => void 
  confirm: () => void 
  
  build() { 
    Column() { 
      Text('添加子目标').fontSize(20).margin({ top: 10, bottom: 10 }).width(Constance.fullSize).margin({left:36,top:16,bottom:10}).fontWeight(600) 
      TextInput({ placeholder: '', text: this.textValue }).height(50).width('90%') 
        .onChange((value: string) => { 
          this.textValue = value 
        }) 
      Flex({ justifyContent: FlexAlign.SpaceAround }) { 
        Button('取消') 
          .onClick(() => { 
            this.controller.close() 
            this.cancel() 
          }).backgroundColor(0xffffff).fontColor($r('app.color.primary_color')) 
        Button('确认') 
          .onClick(() => { 
            this.inputValue = this.textValue 
            this.controller.close() 
            this.confirm() 
          }).backgroundColor(0xffffff).fontColor($r('app.color.primary_color')) 
      }.margin({ bottom: 10 }) 
    } 
    // dialog默认的borderRadius为24vp,如果需要使用border属性,请和borderRadius属性一起使用。 
  } 
}

实现效果

适配的版本信息

IDE:DevEco Studio 4.0.3.600

SDK:HarmoneyOS 4.0.10.11

分享
微博
QQ
微信
回复
2024-05-27 16:18:46
相关问题
组件事件能否到传递组件
865浏览 • 1回复 待解决
组件调用组件的方法
424浏览 • 1回复 待解决
弹窗组件无法调用生命周期接口
669浏览 • 1回复 待解决
如何设置组件随子组件宽度变化
551浏览 • 1回复 待解决
组件与子组件使用@Link双向同步
312浏览 • 1回复 待解决
组件溢出容器问题
459浏览 • 1回复 待解决
组件如何与孙子组件进行状态同步
800浏览 • 1回复 待解决
组件中如何处理子组件内点击事件
1068浏览 • 1回复 待解决
弹窗组件无法进入onPageShow方法
749浏览 • 1回复 待解决
组件的属性是否可以传递Undifined
347浏览 • 1回复 待解决