HarmonyOS 方法里有interface,实现问题

我有一个方法:

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

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

  onError(error: AxiosError): void;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

在外面怎么实现它呢?主要是那个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:()=>{}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

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

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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

MyInterDemo

export interface MyInterDemo {
  func_1: () => void;
  func_2: () => boolean;
  func_3: (arg: string) => void;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
分享
微博
QQ
微信
回复
2024-12-23 19:10:39
相关问题
HarmonyOS interface 问题
905浏览 • 1回复 待解决
HarmonyOS interface中如何定义static方法
652浏览 • 1回复 待解决
HarmonyOS 自定义interface回调问题
947浏览 • 1回复 待解决
如何判断对象是某个interface实现
1828浏览 • 1回复 待解决
HarmonyOS 趋势图的实现方法么?
1199浏览 • 1回复 待解决
HarmonyOS 如何遍历interface
592浏览 • 1回复 待解决
MySQL escape方法问题了解的吗?
3396浏览 • 1回复 待解决
HarmonyOS 关于interface的使用
1662浏览 • 1回复 待解决
interface如何间接导出
1619浏览 • 1回复 待解决
子组件调用父组件方法
1143浏览 • 1回复 待解决
能否使用类似css的calc方法
1305浏览 • 1回复 待解决