HarmonyOS构造函数传入箭头函数,并立即执行 但是并未生效

web 组件类:

constructor(url:string|Resource,count:number, instanceFun:(web:GZWebview)=>void) { 
  super(); 
  this.url = url 
  this.count = count 
  instanceFun(this); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

webPage 类:

instanceFun: (web: GZWebview) => void = (web: GZWebview) => { 
  this.webInstance = web 
  this.registerFunction('setTitleBar',this.setTitleBar) 
} 
registerFunction(name:string,callback:Function){ 
  this.webInstance?.jsbridge.registerHandler(name,callback); 
} 
setTitleBar(name:string,func:Function){ 
  if (func) { 
    let rec:Record<string,BridgeSupportTypes> = {}; 
    rec['code'] = 0; 
    rec['msg'] = '成功'; 
    func(rec); 
  } 
} 
build() { 
  RelativeContainer() { 
    Column() { 
      GZWebview({ url: this.url ,count:this.count ,instanceFun:this.instanceFun})//,webviewController:this.webviewController    // 
        .backgroundColor(Color.Red) 
        .width('100%') 
        .height('100%') 
    }instanceFun: (web: GZWebview) => void = (web: GZWebview) => { 
      this.webInstance = web 
      this.registerFunction('setTitleBar',this.setTitleBar) 
    } 
    registerFunction(name:string,callback:Function){ 
      this.webInstance?.jsbridge.registerHandler(name,callback); 
    } 
    setTitleBar(name:string,func:Function){ 
      if (func) { 
        let rec:Record<string,BridgeSupportTypes> = {}; 
        rec['code'] = 0; 
        rec['msg'] = '成功'; 
        func(rec); 
      } 
    } 
    build() { 
      RelativeContainer() { 
        Column() { 
          GZWebview({ url: this.url ,count:this.count ,instanceFun:this.instanceFun})//,webviewController:this.webviewController    // 
            .backgroundColor(Color.Red) 
            .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.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

instanceFun并未实际调用。

HarmonyOS
2024-08-27 14:05:21
1042浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

struct不支持自定义构造函数,调用相关自定义组件时不会进入构造函数,如果需要完成赋值或者调用方法等操作,建议使用相关装饰器。

分享
微博
QQ
微信
回复
2024-08-27 17:32:32


相关问题
HarmonyOS this和箭头函数相关问题
752浏览 • 1回复 待解决
HarmonyOS ArkTS中如何实现泛型构造函数
1349浏览 • 1回复 待解决
HarmonyOS 如何在builder函数传入组件
917浏览 • 0回复 待解决
HarmonyOS 每秒执行一次的函数
902浏览 • 2回复 待解决
UI组件怎样条件执行选项函数
642浏览 • 1回复 待解决
HarmonyOS setInterval如何立即执行一次
904浏览 • 1回复 待解决
HarmonyOS H5页面使用alert函数生效
633浏览 • 1回复 待解决
如何在ArkTS代码中执行HTML内的JS函数
3124浏览 • 1回复 待解决