HarmonyOS 鸿蒙Next 调用监听方法时报错TypeError: is not callable

发布于 1周前 作者 nodeper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 调用监听方法时报错TypeError: is not callable

在调用监听方法时,报错这个,

cke_4348.png

cke_5010.png


更多关于HarmonyOS 鸿蒙Next 调用监听方法时报错TypeError: is not callable的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

实测预览器点击会报错

但是真机没问题,功能可以使用,也不会报错。

测试代码

@Component
struct Child {
  @Consume @Watch('VideoListen') pageIndex: number
  private videoController: VideoController = new VideoController()
  @State videoSrc: Resource = $rawfile(`234.mp4`)
  @State previewUri: Resource = $r(`app.media.poster1`)
  @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X
  @State isAutoPlay: boolean = false
  @State showControls: boolean = true
  controller: VideoController = new VideoController()
  controller_2: VideoController = new VideoController()

  build() {
    Column() {
      Video({
        src: this.videoSrc,
        previewUri: this.previewUri,
        currentProgressRate: this.curRate,
        controller: this.controller
      })
        .width('100%')
        .height(600)
        .autoPlay(this.isAutoPlay)
        .controls(this.showControls)
        .onStart(() => {
          console.info('onStart')
        })
        .onPause(() => {
          console.info('onPause')
        })
        .onFinish(() => {
          console.info('onFinish')
        })
        .onError(() => {
          console.info('onError')
        })

    }
  }

  VideoListen() {
    this.controller.start()
  }
}

@Entry
@Component
struct Page012 {
  @Provide pageIndex: number = 0
  @State message: string = '播放';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.pageIndex = 1
          })
        Child()
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next 调用监听方法时报错TypeError: is not callable的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


好吧,我这个也是预览器报错,但模拟器可以正常运行

在HarmonyOS鸿蒙Next中,调用监听方法时出现TypeError: is not callable错误,通常是因为尝试调用了一个不可调用的对象。可能的原因包括:

  1. 方法未正确定义:确保监听方法在类或对象中正确定义,且方法名拼写正确。
  2. 方法未绑定:如果监听方法是一个类方法,确保在调用时正确绑定到实例上。
  3. 方法被覆盖或重命名:检查是否有其他代码覆盖或重命名了该方法。
  4. 方法未导入或未声明:确保方法在调用前已正确导入或声明。

例如,以下代码可能导致该错误:

class MyClass {
  myMethod() {
    console.log("Method called");
  }
}

const instance = new MyClass();
const method = instance.myMethod; // 未绑定到实例
method(); // TypeError: method is not callable

正确的方式是:

class MyClass {
  myMethod() {
    console.log("Method called");
  }
}

const instance = new MyClass();
instance.myMethod(); // 正确调用

确保监听方法在调用时是可调用的,且正确绑定到实例上。

在使用HarmonyOS鸿蒙Next时,如果调用监听方法时报错TypeError: is not callable,通常是因为尝试调用的对象不是一个函数。可能的原因包括:

  1. 方法名拼写错误:检查方法名是否正确,确保没有拼写错误。
  2. 方法未定义:确认该方法是否在类或对象中正确定义。
  3. 方法未正确绑定:如果方法依赖于this上下文,确保它已正确绑定到当前对象。
  4. 变量覆盖:检查是否有变量覆盖了方法名,导致其不再是函数。

建议仔细检查代码,确保调用的方法是一个可调用的函数。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!