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[] = []; //答案选项列表
}
//直接代码中调用(不管chooseIds中有没有数据,都调用clear)
this.preData.chooseIds.clear()
//报错 TypeError: Cannot read property clear of undefined
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%')
  }
}

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

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