HarmonyOS鸿蒙Next中父组件可以直接调用子组件的函数么

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS鸿蒙Next中父组件可以直接调用子组件的函数么 鸿蒙里面 父组件可以直接调用子组件的函数么

5 回复

这个确实无法实现,不过你可以通过其它的路径进行实现,比如很简单的监听,在子组件中定义一个观察变化的属性,V1版本使用@Watch装饰器,V2版本可以使用@Monitor装饰器。

V1版本使用@Watch装饰器

struct Child {
  @Prop [@Watch](/user/Watch)("test") testListener: boolean = false

  /**
   *AUTHOR:AbnerMing
   *INTRODUCE:子组件方法
   */
  test() {
    console.log("===调用了子组件的test方法")
  }

  build() {
    Text("我是子组件")
      .width("100%")
      .height(50)
      .backgroundColor(Color.Pink)
      .textAlign(TextAlign.Center)
  }
}

@Entry
@Component
struct DemoPage {
  @State testListener: boolean = false

  build() {
    Column() {
      Child({ testListener: this.testListener })
      Button("点击")
        .onClick(() => {
          this.testListener = !this.testListener
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

V2版本可以使用@Monitor装饰器

struct Child {
  @Param testListener: boolean = false

  /**
   *AUTHOR:AbnerMing
   *INTRODUCE:子组件方法
   */
  [@Monitor](/user/Monitor)("testListener")
  test() {
    console.log("===调用了子组件的test方法")
  }

  build() {
    Text("我是子组件")
      .width("100%")
      .height(50)
      .backgroundColor(Color.Pink)
      .textAlign(TextAlign.Center)
  }
}

@Entry
@ComponentV2
struct DemoPage {
  @Local testListener: boolean = false

  build() {
    Column() {
      Child({ testListener: this.testListener })
      Button("点击")
        .onClick(() => {
          this.testListener = !this.testListener
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

更多关于HarmonyOS鸿蒙Next中父组件可以直接调用子组件的函数么的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


不行,我试过好多方法,ArkTs本身就是声明式组件,不支持绑定试调用组件方法,我们用的都是回调和广播通知,也可以通过以下方法实现类似功能:

@Component
struct Child {
  @State private text: string = '初始值';
  private controller: ChildController = new ChildController();

  aboutToAppear() {
    if (this.controller) {
      // 给controller对应的方法赋值
      this.controller.changeText = this.changeText;
    }
  }

  private changeText = (value: string) => {
    console.log('测试的1')
    this.text = value;
  }

  build() {
    Column() {
      Text(this.text)
    }
  }
}

// 定义controller对象
class ChildController {
  changeText = (value: string) => {}
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello ArkUi';
  @State inputValue: string = ''
  private ChildRef = new ChildController();

  build() {
      Column({space: 20}) {
          // 将new之后的controller对象传入子组件
          Child({ controller: this.ChildRef }) 
          Button('测试').onClick(()=>{
            this.ChildRef.changeText('Parent调用child的changeText');
          })
      }
      .width('100%')
      .padding(20)
  }
}

你好。

不可以。鸿蒙是arkUI框架,声明式布局,没办法直接操作组件对象,调用其内部函数。

但是可以从别的方式实现该效果。例如使用eventhub通知广播,使用注册回调,使用单例等。

望采纳。

在HarmonyOS(鸿蒙)Next中,父组件可以通过@Link@Provide@Consume装饰器与子组件进行数据绑定,从而间接调用子组件的函数。父组件不能直接调用子组件的函数,但可以通过状态管理或事件机制触发子组件的函数执行。例如,父组件可以通过@Link绑定子组件的状态,当父组件修改该状态时,子组件的响应函数会被触发。

在HarmonyOS鸿蒙Next中,父组件通常不能直接调用子组件的函数。组件间的通信应通过事件或状态管理来实现,以确保组件的独立性和可维护性。父组件可以通过@State@Prop等方式传递数据,子组件可以通过事件回调与父组件通信。这种设计符合组件化开发原则,确保代码的模块化和可复用性。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!