HarmonyOS List提示undefined

类中定义了一个chooseIds字段列表属性,调用clear方法却提示undefined,代码如下:

import { List } from '@kit.ArkTS';

export class HealthEvaluationBean {
  id: number = -1; //题目ID
  qmId: number = -1; //试卷ID
  title: string = '';
  type: number = -1; //选择类型 1单选 2多选
  needChoice: number = -1; //是否必须选择 0否 1是
  backgroundImg: string = ''; //背景图
  chooseIds: List<number> = new List<number>(); //记录底下列表的选择项,以此来判断单选的
  systemQuestionDetailOptionVoList: SystemQuestionDetailOptionVoListBean[] = []; //答案选项列表
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
//直接代码中调用(不管chooseIds中有没有数据,都调用clear)
this.preData.chooseIds.clear()
  • 1.
  • 2.
//报错 TypeError: Cannot read property clear of undefined
  • 1.
HarmonyOS
2025-01-09 16:18:55
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考示例如下:

import { List } from '@kit.ArkTS';

class HealthEvaluationBean {
  id: number = -1; //题目ID
  qmId: number = -1; //试卷ID
  title: string = '';
  type: number = -1; //选择类型 1单选 2多选
  needChoice: number = -1; //是否必须选择 0否 1是
  backgroundImg: string = ''; //背景图
  chooseIds: List<number> = new List<number>(); //记录底下列表的选择项,以此来判断单选的
}

@Entry
@Component
struct TestPage {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Button("test bean").onClick(() => {
        let data = new HealthEvaluationBean()
        data.chooseIds.clear()
      })
    }
    .height('100%')
    .width('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.

需要实例化JSON.parse()并不会实例化对象。

分享
微博
QQ
微信
回复
2025-01-09 18:46:28
相关问题
HarmonyOS创建前置广角相机返回undefined
766浏览 • 1回复 待解决
HarmonyOS SourceTool.xxx值为undefined
745浏览 • 1回复 待解决
使用fetch报错,fetch undefined
8049浏览 • 2回复 待解决
HarmonyOS List嵌套ListList嵌套Grid问题
696浏览 • 1回复 待解决
如何获取单例为undefined
1161浏览 • 1回复 待解决
建前置广角相机返回undefined
189浏览 • 0回复 待解决
HarmonyOS List嵌套list中的布局
693浏览 • 1回复 待解决
openssl库编译会报undefined symbol: __errno
1907浏览 • 1回复 待解决
undefined symbol napi_create_threadsafe_function
5006浏览 • 1回复 待解决