鸿蒙 API 9和API10使用@Prop 向子组件传递class类型参数是有区别的。
1,API9上传递类型 必须是字符串、数字或布尔类型,不包含对象
2,API10上传递类型 包括对象。
api9对应的 devceo studio版本是 3.1版本,api10 可以下载deveco studio 4.0的版本使用api10
如下是API10版本下的 使用@Prop 向子组件传递class类型参数
// 主入口 @Entry @Component struct Prop_component_Page { @State message: string = 'Hello World'
build() { Row() { Column() { // 子组件 Component_Page({bean: new PersonalContent("黑板报","鸿蒙开发技术")}) } .width('100%') } .height('100%') } }
// 自定义子组件 @Component struct Component_Page { @Prop bean: PersonalContent // api9版本情况下 会提示不支持对象
build() { Row() { Column() { Text(this.bean.name) .fontSize(50) .fontWeight(FontWeight.Bold) } .width('100%') } .height('100%') } }
// 对象 export class PersonalContent{ name:string content:string
constructor(name:string,content:string) { this.name = name this.content = content } }
有兴趣可以加入 星球: 玩转鸿蒙应用开发