HarmonyOS 方法里有interface,实现问题

我有一个方法:

public static searchPageData(o: string, callBack: OnCallBack<BaseResultBean<SearchBean>>)

export interface OnCallBack<T = object> {
  onSuccess(bean: T): void;

  onError(error: AxiosError): void;
}

在外面怎么实现它呢?主要是那个interface,看怎么实现。

HttpUtil.searchPageData(“harmonyos”,{这里})

HarmonyOS
2024-12-23 15:45:19
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

ArkTS不支持匿名类,建议使用嵌套类实现。因为使用匿名类创建的对象类型未知,这与ArkTS不支持structural typing和对象字面量的类型冲突。

// 原先

class A {
  foo() {
    let a = new class {
      v: number = 123
    }();
  }
}
// 现在

class A {
  foo() {
    class B {
      v: number = 123
    }
    let b = new B();
  }
}
//或者

export interface IVoiceRecordListener<T> {
  onSucces:(t: T)=> void
  onFailed:(code: string, reason: string)=>void
}

let obj: IVoiceRecordListener<string> = {
  onSucces:()=>{},
  onFailed:()=>{}
}

关于使用方法可以参考如下示例:

import { MyInterDemo } from '../interface/MyInterDemo'

@Entry
@Component
struct TestInterfacePage {

  testInter(inter: MyInterDemo) {
    console.log('testInter');
    inter.func_1();
    inter.func_2();
    inter.func_3('hello');
  }

  build() {
    Row() {
      Column() {
        Button('Test interface')
          .onClick(() => {
            let demo: MyInterDemo = {
              func_1: () => {
                console.log('func_1');
              },
              func_2: () => {
                console.log('func_2');
                return true;
              },
              func_3: (arg: string) => {
                console.log('func_3:' + arg);
              }
            }
            this.testInter(demo);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

MyInterDemo

export interface MyInterDemo {
  func_1: () => void;
  func_2: () => boolean;
  func_3: (arg: string) => void;
}
分享
微博
QQ
微信
回复
2024-12-23 19:10:39
相关问题
HarmonyOS interface 问题
522浏览 • 1回复 待解决
HarmonyOS interface中如何定义static方法
297浏览 • 1回复 待解决
HarmonyOS 自定义interface回调问题
535浏览 • 1回复 待解决
如何判断对象是某个interface实现
1407浏览 • 1回复 待解决
HarmonyOS 趋势图的实现方法么?
692浏览 • 1回复 待解决
HarmonyOS 如何遍历interface
249浏览 • 1回复 待解决
MySQL escape方法问题了解的吗?
2950浏览 • 1回复 待解决
HarmonyOS 关于interface的使用
1271浏览 • 1回复 待解决
interface如何间接导出
1296浏览 • 1回复 待解决
interface回调如何调用
1094浏览 • 1回复 待解决
子组件调用父组件方法
734浏览 • 1回复 待解决
能否使用类似css的calc方法
799浏览 • 1回复 待解决