HarmonyOS 鸿蒙Next:api11类型怎么定义啊,想定义一个对象数组
HarmonyOS 鸿蒙Next:api11类型怎么定义啊,想定义一个对象数组
刚把项目更新到api11,但是定义类型犯了难,求大佬解答
代码
export interface position {
name: string
id: number
children?: position[]
}
class MyBean {
name?: string
imgSrc?: string
isOnline?: boolean
sex?: number
position?: string
}
@Entry
@Component
struct Page174 {
private listData: position[] = [
{
name: ‘湖南省审计厅’, id: 10000, children: [
{ name: ‘湖南省审计厅本级’, id: 11000, },
{
name: ‘湖南省审计厅下级’, id: 12000, children: [
{
name: ‘湘潭市审计局’, id: 12100, children: [
{ name: ‘湘潭市审计局本级’, id: 12110 },
{
name: ‘湘潭市审计局下级’, id: 12120, children: [
{ name: ‘湘潭市审计局下级1’, id: 12121 },
{ name: ‘湘潭市审计局下级1’, id: 12122 }
]
},
]
},
{ name: ‘长沙市审计局’, id: 12000 }]
}
]
}]
private userList: Record<string, string | boolean | number>[] = [
{
‘name’: ‘用户名1’,
‘imgSrc’: ‘person (1).jpg’,
‘isOnline’: true,
‘sex’: 0,
‘position’: ‘职位名称1’
},
{
‘name’: ‘用户名2’,
‘imgSrc’: ‘person (2).jpg’,
‘isOnline’: false,
‘sex’: 1,
‘position’: ‘职位名称2’
}
]
private userList_2: MyBean[] = [
{
name: ‘用户名1’,
imgSrc: ‘person (1).jpg’,
isOnline: true,
sex: 0,
position: ‘职位名称1’
},
{
name: ‘用户名2’,
imgSrc: ‘person (2).jpg’,
isOnline: false,
sex: 1,
position: ‘职位名称2’
}
]
build() {
Column() {
Button(‘测试’).onClick(() => {
console.info(‘this.listData’, JSON.stringify(this.listData, null, ‘–’))
console.info(‘name 1:’, this.userList[1][‘name’])
console.info(‘name 2:’, this.userList_2[0].name)
})
}
.width(‘100%’)
.height(‘100%’)
}
}
打印
app Log: this.listData [
–{
----“name”: “湖南省审计厅”,
----“id”: 10000,
----“children”: [
------{
--------“name”: “湖南省审计厅本级”,
--------“id”: 11000
------},
------{
--------“name”: “湖南省审计厅下级”,
--------“id”: 12000,
--------“children”: [
----------{
------------“name”: “湘潭市审计局”,
------------“id”: 12100,
------------“children”: [
--------------{
----------------“name”: “湘潭市审计局本级”,
----------------“id”: 12110
--------------},
--------------{
----------------“name”: “湘潭市审计局下级”,
----------------“id”: 12120,
----------------“children”: [
------------------{
--------------------“name”: “湘潭市审计局下级1”,
--------------------“id”: 12121
------------------},
------------------{
--------------------“name”: “湘潭市审计局下级1”,
--------------------“id”: 12122
------------------}
----------------]
--------------}
------------]
----------},
----------{
------------“name”: “长沙市审计局”,
------------“id”: 12000
----------}
--------]
------}
----]
–}
]
app Log: name 1: 用户名2
app Log: name 2: 用户名1
大佬麻烦帮我看一下我新发的贴子那个问题
说错了是api10
不行,还是报错了,Property ‘children’ is missing in type ‘{ name: string; id: number; }’ but required in type ‘position’.
因为有的children是没有children的
可以参考适配指导案例修改
如果还是不会修改,那把报错代码片段贴出来,我帮你改。
private listData:position[]= [{ name: ‘湖南省审计厅’,id:10000,children:[ {name:‘湖南省审计厅本级’,id:11000,}, {name:‘湖南省审计厅下级’,id:12000,children:[ {name:‘湘潭市审计局’,id:12100,children:[ { name: ‘湘潭市审计局本级’,id:12110}, { name: ‘湘潭市审计局下级’,id:12120,children:[ { name: ‘湘潭市审计局下级1’,id:12121}, { name: ‘湘潭市审计局下级1’,id:12122} ]}, ]}, {name:‘长沙市审计局’,id:12000}]} ] }]
private userList=[ {name:‘用户名1’,imgSrc:‘person (1).jpg’,isOnline:true,sex:0,position:‘职位名称1’}, {name:‘用户名2’,imgSrc:‘person (2).jpg’,isOnline:false,sex:1,position:‘职位名称2’} ]
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17
export interface position{ name:string, id:number, children:position[] }
在HarmonyOS鸿蒙Next中,API 11级别下定义对象数组的方式与大多数面向对象的编程语言类似。你可以通过定义一个类,然后创建该类的数组来实现。以下是一个基本的示例,展示如何定义一个对象数组:
-
定义类: 首先,定义一个类。例如,一个简单的
Person
类:public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } // Getter and Setter methods public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
-
定义对象数组: 然后,在需要的地方创建
Person
类的数组:Person[] people = new Person[10]; people[0] = new Person("Alice", 30); people[1] = new Person("Bob", 25); // 初始化其他元素...
这样,你就定义了一个Person
对象数组。Harmonyos Next Api12讲解:https://www.itying.com/category-93-b0.html