在ChildComponent组件中 model的接收是一定会传参数的,但是在写组件时一定让写默认值,导致很多地方都需要判空
@ObservedV2
class Person {
age : number = 0
name : string = '小红'
constructor(age: number, name: string) {
this.age = age;
this.name = name;
}
}
@Entry
@ComponentV2
struct Index {
@Local showList : Array<Person> = [ new Person(12 , '张三') , new Person(28 , '李四') ]
build() {
Column(){
ForEach(this.showList , (person : Person) => {
ChildComponent({
person : person
})
})
}
}
}
@ComponentV2
struct ChildComponent {
person : Person | null = null
build() {
Column(){
}
.width('100%')
.height(100)
}
}
请参考示例: