HarmonyOS 'Function.bind' is not supported (arkts-no-func-bind)的警告

自定义了一个组件,写了this.componentBuilder.bind(this),然后编译的时候报了

Function.bind’ is not supported (arkts-no-func-bind) 的警告

HarmonyOS
2024-12-25 07:58:27
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

不支持bind,在ArkTS中,this的语义仅限于传统的OOP风格,函数体中禁止使用this,不支持在函数中使用this。

替代方案: 使用() => {this.xxx()} 绑定this的作用域

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/typescript-to-arkts-migration-guide-V5#不支持functionbind

请参考示例修改:component:():void=>{this.contentBuilder()}

支持文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builderparam-V5#初始化builderparam装饰的方法

import { NavTitle } from '../view/TitleView';
import { router } from '@kit.ArkUI';
import { TemplateView } from '../view/TemplateView';
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebView {
  private backFun = () => {
    router.back()
  }
  @State pageStatus: number = 0
  private webviewController: web_webview.WebviewController = new web_webview.WebviewController()

  @Builder
  componentBuilder() {
    Web({
      src: $rawfile("index.html"),
      controller: this.webviewController,
    })
      .onPageEnd(() => {
        this.pageStatus = 1
      })
  }
  @BuilderParam contentBuilder:()=> void = this.componentBuilder
  build() {

    Column() {
      NavTitle({
        backFun: () => {
          this.backFun()
        }
      })

      TemplateView({
        pageStatus: this.pageStatus,
        //绑定父类的this,正常显示
        // component: this.componentBuilder.bind(this)


        //显示不正常
        // component: this.contentBuilder
        component:():void=>{this.contentBuilder()}
      })
    }
    .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.
  • 46.
  • 47.
  • 48.
  • 49.
分享
微博
QQ
微信
回复
2024-12-25 11:11:49
相关问题
HarmonyOS 函数使用bind提示警告
1078浏览 • 1回复 待解决
HarmonyOS 关于Funtionbind
455浏览 • 1回复 待解决
HarmonyOS bind提示Address not available
617浏览 • 1回复 待解决
HarmonyOS bind(this)问题替代方案
1039浏览 • 1回复 待解决
HarmonyOS UDP socket bind失败
796浏览 • 1回复 待解决
HarmonyOS TLSSocket为什么强制需要bind
824浏览 • 1回复 待解决
针对IPv6地址TLSSocket bind操作无响应
1096浏览 • 1回复 待解决
HarmonyOS 使用List警告
963浏览 • 1回复 待解决
HarmonyOS 警告'libzhihui.so' is not verified
786浏览 • 1回复 待解决