
回复
目录
ArkTS语言的使用在生成器函数中存在一定的限制:
上述限制都不适用于事件处理函数(例如onClick)的匿名函数实现。
ArkTS支持通过$$双向绑定变量,通常应用于状态值频繁改变的变量。
@Entry
@Component
struct bindPopup {
@State customPopup: boolean = false
build() {
Column() {
Button(){
Text('Popup')
}
.onClick(()=>{
this.customPopup = !this.customPopup
})
.bindPopup(
$$this.customPopup, {
message: "showPopup"
组件的成员变量可以通过两种方式初始化:
@State counter: Counter = new Counter()
MyComponent({counter: $myCounter})
装饰器类型 | 本地初始化 | 通过构造函数参数初始化 |
@State | 必须 | 可选 |
@Prop | 禁止 | 必须 |
@Link | 禁止 | 必须 |
@StorageLink | 必须 | 禁止 |
@StorageProp | 必须 | 禁止 |
@Provide | 必须 | 可选 |
@Consume | 禁止 | 禁止 |
@ObjectLink | 禁止 | 必须 |
常规成员变量 | 推荐 | 可选 |
从上表中可以看出:
通过构造函数方法初始化成员变量,需要遵循如下规则:
从父组件中的变量(下)到子组件中的变量(右) | @State | @Link | @Prop | 常规变量 |
@State | 不允许 | 允许 | 允许 | 允许 |
@Link | 不允许 | 允许 | 不推荐 | 允许 |
@Prop | 不允许 | 不允许 | 允许 | 允许 |
@StorageLink | 不允许 | 允许 | 不允许 | 允许 |
@StorageProp | 不允许 | 不允许 | 不允许 | 允许 |
常规变量 | 允许 | 不允许 | 不允许 | 允许 |
从上表中可以看出:
感谢各位大佬支持!!!
互三啦!!!