鸿蒙开发TypeScript第十一课:Decorator装饰器
比如:@Entry,@Component都是装饰器。它会在Lesson_11_decorator_Page运行之前先跑@Component,@Entry。有需要完整教程demo的私信我,我每天都看私信的。鸿蒙开发中大量的Decorator装饰器。
·
鸿蒙开发TypeScript第十一课:Decorator装饰器
鸿蒙开发中大量的Decorator装饰器
比如:@Entry,@Component都是装饰器。它会在Lesson_11_decorator_Page运行之前先跑@Component,@Entry
直接上示例代码:
// 组件装饰器,不使用泛型
// 组件装饰器,使用箭头函数
function ComponentStyle(style: string) {
return (target: Function): void => {
target.prototype.getStyle = (): string => {
return style;
};
};
}
// 1. 简单的类装饰器
function Greeter(target: Function): void {
target.prototype.greet = (): void => {
console.log('你好');
};
}
@Greeter
class User1 {
greet() {
throw new Error('Method not implemented.');
}
name: string;
constructor(name: string) {
this.name = name;
}
}
@Entry
@Component
@ComponentStyle("你好")
struct Lesson_11_decorator_Page {
@State message: string = 'Lesson_11_decorator_Page';
aboutToAppear(): void {
// 使用
let user = new User1("张三");
user.greet(); // 你好
}
教程项目的全部源码图:

有需要完整教程demo的私信我,我每天都看私信的。
更多推荐



所有评论(0)