往期鸿蒙全套实战文章必看:(附带鸿蒙全栈学习资料)
ArkUI是否支持自定义装饰器
ArkUI可以自定义装饰器,示例代码中@decorateKlass为自定义类装饰器,@MyDescriptor为自定义方法装饰器。
参考代码如下:
interface CustomStr {
name: string
}
function decorateKlass(_target: CustomStr) {
}
@decorateKlass
class Person {
name: string = '自定义装饰器';
}
function MyDescriptor(target: Object, key: string, descriptor: PropertyDescriptor) {
const originalMethod: Function = descriptor.value;
descriptor.value = (...args: Object[]) => {
if (canIUse('SystemCapability.ArkUI.ArkUI.Full')) {
console.info(`Calling ${target.constructor.name} method ${key} with argument: ${args}`);
}
const result: Object = originalMethod(...args);
return result;
}
return descriptor;
}
@Entry
@Component
struct DecoratorDemo {
@State person: string = '';
aboutToAppear() {
this.person = this.demo();
}
build() {
Column() {
Flex({ justifyContent: FlexAlign.Center }) {
Text(this.person)
}
.backgroundColor(Color.Gray)
}
.height('100%')
.width('100%')
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center)
}
@MyDescriptor
demo() {
let person = new Person();
return person.name;
}
}

209

被折叠的 条评论
为什么被折叠?



