如何在组件函数传递想要在函数结束后处理独有的业务逻辑

是汉堡黄
发布于 2025-11-2 22:33
浏览
0收藏

v1的处理方式

@Entry
@Component
struct Index {
  build() {
    Column() {
      TestDemo({
        callBack: (callBack: () => void) => {
          console.info('方法处理')
          callBack()
        }
      })
    }
    .size({ width: '100%', height: '100%' })
  }
}

@Component
struct TestDemo {
  callBack: (callBack: () => void) => void = () => {}
  build() {
    Column() {
      Button('点击弹起').onClick((event: ClickEvent) => {
        this.callBack(() => {
          console.info('独有业务逻辑')
        })
      })
    }
    .size({ width: '100%', height: '100%' })
  }
}

v2的处理方式

@Entry
@ComponentV2
struct Index {
  build() {
    Column() {
      TestDemo({
        callBack: (callBack: () => void) => {
          console.info('方法处理')
          callBack()
        }
      })
    }
    .size({ width: '100%', height: '100%' })
  }
}

@ComponentV2
struct TestDemo {
  @Param callBack: (callBack: () => void) => void = () => {}
  build() {
    Column() {
      Button('点击弹起').onClick((event: ClickEvent) => {
        this.callBack(() => {
          console.info('独有业务逻辑')
        })
      })
    }
    .size({ width: '100%', height: '100%' })
  }
}

分类
收藏
回复
举报
回复
    相关推荐