以下是类装饰器、属性装饰器、方法装饰器、参数装饰器的简单示例:
@Entry
@Component
struct DecoratorDemo {
@State address: string = '点我探索世界';
build() {
RelativeContainer() {
Text(this.address)
.fontSize(15)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.address = new ExploringWorld().TestFun('中国');
});
}
.height('100%')
.width('100%');
}
}
// 类装饰器
function TestClassDecorator(target: Function) {
if (target) {
}
}
// 属性装饰器
function TestMemberDecorator(target: ExploringWorld, memberName: String) {
if (target && memberName) {
}
}
// 方法装饰器
function TestFunDecorator(target: ExploringWorld, propertyName: String, descriptor: PropertyDescriptor) {
if (target && propertyName && descriptor) {
}
}
// 参数装饰器
function TestArgDecorator(target: Function, methodName: String, paramIndex: Number) {
if (target && methodName && paramIndex) {
}
}
@TestClassDecorator
class ExploringWorld {
@TestMemberDecorator
count: number = 123;
@TestFunDecorator
TestFun(@TestArgDecorator param: string): string {
if (param) {
return '欢迎来到大美中国';
} else {
return '未知地点';
}
}
}