定义了一个视图组件
@Component
export struct HealthLifeComponent {
private url: string = ''
}
在View层,按照下方代码引入使用:
HealthLifeComponent({ url: 'xxxxxxxx' })
在编译时出现告警提示:Property 'url' is private and can not be initialized through the component constructor. 请问是什么问题?
这个编译告警的意思是你通过组件构造函数初始化一个私有属性 url,但是 ArkTS 中是不行的。你在属性名前使用 private 关键字,这是一个私有属性,不能在组件构造函数中直接初始化。
解决方法:声明为公共属性,然后在组件构造函数或其他地方进行初始化。例如: