HarmonyOS 如何定义一个根容器

怎么定义一个根组件/容器,类似 Column/Row 之类的。

HarmonyOS
2024-12-20 15:55:41
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

ArkTS支持TS5.0之前的TS装饰器语法。关于装饰器的定义和运行时行为,可以参考TS官方文档:https://www.typescriptlang.org/docs/handbook/decorators.html注意,如果在ets文件中定义装饰器,则需要同时满足ArkTS的语法规则,比如不能使用any等。以下是类装饰器、属性装饰器、方法装饰器、参数装饰器的简单示例:

function TestClassDecorator (target: Function) {}
function TestMemberDecorator (target: testClass, memberName: String) {}
function TestFunDecorator (target: testClass, propertyName: String, descriptor: PropertyDescriptor) {}
function TestArgDecorator (target: Function, methodName: String, paramIndex: Number) {}
@TestClassDecorator
class testClass {
@TestMemberDecorator
count: number = 123;
@TestFunDecorator
TestFun(@TestArgDecorator param: string) {}
}
请参考
import { MyDescriptor } from ‘…/common/decorators’
@Entry
@Component
struct Index_231129191721035 {
@State message: string = ‘Hello World’;
aboutToAppear() {
this.demo()
}
build() {
Flex() {
}
.backgroundColor(Color.Green)
.constraintSize({
minWidth: 100,
maxWidth: 200,
minHeight: 0,
maxHeight: 200
})
.height(‘100%’)
}
@MyDescriptor
demo() {
console.log(‘ccc’)
return ‘cccc’
}
}
export function MyDescriptor(target: Object, key: string, descriptor: PropertyDescriptor) {
const originalMethod: Function = descriptor.value
descriptor.value = (…args: Object[]) => {
console.log(Calling ${target.constructor.name} method ${key} with argument: ${args})
const result: Object = originalMethod(…args)
console.log(Method ${key} returned: ${result})
return result
}
return descriptor
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.

装饰器装饰的类A,需要new A才能触发装饰器。

分享
微博
QQ
微信
回复
2024-12-20 18:54:25
相关问题
如何定义一个未知类型的对象
1776浏览 • 2回复 待解决
HarmonyOS 如何定义一个可空的范型
534浏览 • 1回复 待解决
如何实现一个定义询问框
1202浏览 • 1回复 待解决
HarmonyOS定义一个map为临时变量
1063浏览 • 1回复 待解决
如何快速开发出一个定义弹窗?
1144浏览 • 1回复 待解决
HarmonyOS 实现一个定义分类列表
1195浏览 • 1回复 待解决
HarmonyOS定义组件如何绘制一个三角
1050浏览 • 1回复 待解决
如何实现一个定义样式的toast提示
2785浏览 • 1回复 待解决
如何封装一个定义Dialog对话框
3058浏览 • 1回复 待解决
HarmonyOS一个定义的tabs冲突
970浏览 • 1回复 待解决
如何在全局实现一个定义dialog弹窗
3673浏览 • 1回复 待解决