Property 'WSD_ON' does not exist on type 'SwitchCon'. Did you mean to access the static member 'SwitchCon.WSD_ON' instead? <ArkTSCheck>

SwitchCon类型上不存在WSD_ON属性。你是不是想访问静态成员SwitchCon.WSD_ON呢? <ArkTSCheck>

这个错误信息来自 TypeScript 或者类似 TypeScript 的语言(比如鸿蒙系统开发中使用的 ArkTS 语言 )。它意味着你正在尝试从SwitchCon类的一个实例上去访问WSD_ON属性,但这个属性并不是实例属性。编译器提示你,也许你本意是把它当作SwitchCon类的静态属性来访问。

export class SwitchCon {
  //卧室
  static readonly WS_ON: string = "W0" //卧室总开关 0关 1开
  static readonly WS_OFF: string = "W1" //卧室总开关 0关 1开
  static readonly WSD_ON: string = "WD0" //卧室灯 0关 1开
  static readonly WSD_OFF: string = "WD1" //卧室灯 0关 1开
}

export const switchCon = new SwitchCon()

 有一处类型检查相关的问题容易引发报错,因为你把类的属性都定义成了静态的,然后又实例化了这个类。在使用这些属性时,应该通过类名访问静态属性,而不是实例。

export class SwitchCon {
    // 卧室
    static readonly WS_ON: string = "W0"; // 卧室总开关 0关 1开
    static readonly WS_OFF: string = "W1"; // 卧室总开关 0关 1开
    static readonly WSD_ON: string = "WD0"; // 卧室灯 0关 1开
    static readonly WSD_OFF: string = "WD1"; // 卧室灯 0关 1开
}

// 使用示例
console.log(SwitchCon.WS_ON);

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐