HarmonyOS 鸿蒙Next 如何像vue和react一样暴露方法

HarmonyOS 鸿蒙Next 如何像vue和react一样暴露方法

原因

因为本人现在是就职前端(react、vue)开发,迫于公司业务正在学习使用arkTs开发,但在学习过程中发现了一些问题。我发现在使用自定义组件时我无法获取组件的暴露的方法,个人认为这个在写组件时挺重要的,因此我闭关冥想了两天,自己实现一个demo,大佬可以帮我看看写的如何,欢迎留言。

解决方案

直接贴代码:

index.ets

Child组件

import test from './test'

export  type  exposeMethodsType = {
  changeText: (value:string) => void
}

@Component
struct Child {
  @State private text: string = '初始值'
  exposeMethods: (methods:exposeMethodsType) => void // 1.定义暴露方法的成员变量
  aboutToAppear() {
    // 2.在组件初始化时调用exposeMethods并通过参数的形式暴露成员方法changeText
    this.exposeMethods({
      changeText: this.changeText.bind(this)
    })
  }
  // 暴露的方法
  changeText(value:string) {
      this.text = value
  }

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

Parent组件

export let ChildRef:exposeMethodsType = null
@Component
@Entry
struct Parent {
  @State private ref: exposeMethodsType = null
  // 4.通过参数获取并赋值给ref
  onLoad(methods) {
    ChildRef = methods
    this.ref = methods
  }

  build() {
    Column() {
      Text('获取Child的exposeMethods!').fontSize('18vp').fontColor(Color.Gray)
      Divider()
      // 3.传入方法
      Child的({ exposeMethods: this.onLoad.bind(this) })
      Button('Parent调用childer的changeText').onClick(() => {
        this.ref.changeText('我被Parent调用了!')
      })
      test()
    }
  }
}

test.ets

在其他文件调用导出的ChildRef调用changeText修改Child

import {exposeMethodsType,ChildRef} from './Index'
@Component
export default  struct test {
  build(){
    Row(){
      Button('test调用childer的changeText').onClick(() => {
        ChildRef.changeText('我被test调用了!')
      })
    }
  }
}

结果

image image image

废除demo

@Component
struct Child {
  @Link  private items: number[];
  @State private text: string = 'sdad'
  @Link private  getInstance: Child
  onLoad:() => void
  aboutToAppear() {
    this.getInstance = this
    this.onLoad()
  }

  changeText() {
    setTimeout(() => {
      this.text = 'ccc'
    },3000)
  }

  build() {
    Column() {
      Text(this.text)
      Button(`Button1: push`).onClick(() => {
        this.items.push(this.items.length + 1);
      })
      Button(`Button2: replace whole item`).onClick(() => {
        this.items = [100, 200, 300];
      })
    }
  }
}

@Entry
@Component
struct Parent {
  @State arr: number[] = [1, 2, 3];
  @State readonly ref: Child = null
  onLoad() {
    this.ref.changeText()
    // this.ref.
    // this.ref = new Child()
  }
  build() {
    Column() {
      Child({ items: $arr, getInstance: $ref,onLoad: this.onLoad.bind(this)})
      ForEach(this.arr,
        item => {
          Text(`${item}`)
        },
        item => item.toString()
      )
      Button('点我').onClick(() => {
        this.ref.changeText()
      })
    }
  }
}

更多关于HarmonyOS 鸿蒙Next 如何像vue和react一样暴露方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复
这样是可行,但是这样写不好统一管理暴露的方法吧!。

更多关于HarmonyOS 鸿蒙Next 如何像vue和react一样暴露方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


感觉整得挺麻烦的,arkui 本身有提供 ‘@ohos.events.emitter’ 作为事件交互的方法,你只需要在子组件注册监听,在父组件调用事件就行

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

emitter可以对标vue的eventBus,不是说用这种事件总线不行,而是组件调用的链路会变得不够清晰。

在HarmonyOS鸿蒙Next中,可以通过@Component装饰器来定义组件,并通过export关键字暴露方法。具体步骤如下:

  1. 定义组件:使用@Component装饰器定义一个组件,并在组件内部编写需要暴露的方法。
  2. 暴露方法:在组件类中,通过export关键字将方法暴露出去,供外部调用。

示例代码如下:

@Component
export default class MyComponent {
  // 定义一个方法
  @Method
  public myMethod(param: string): void {
    console.log("Method called with param:", param);
  }
}

在这个示例中,myMethod方法通过@Method装饰器标记为可暴露的方法,外部可以通过组件的实例调用该方法。

  1. 调用暴露的方法:在外部使用该组件时,可以通过组件的实例调用暴露的方法。
const myComponent = new MyComponent();
myComponent.myMethod("Hello, HarmonyOS");

这样,myMethod方法就可以像Vue和React中的方法一样被外部调用。

回到顶部