当前位置: 首页 > news >正文

第十九章 TypeScript 装饰器Decorator

Decorator 装饰器是一项实验性特性,在未来的版本中可能会发生改变

它们不仅增加了代码的可读性,清晰地表达了意图,而且提供一种方便的手段,增加或修改类的功能

若要启用实验性的装饰器特性,你必须在命令行或tsconfig.json里启用编译器选项

类装饰器  ClassDecorator

/*** 类装饰器  ClassDecorator* @param target  形参  target 是形参,可以是任何名字* @param  result  返回结果:构造函数* @param name* */
// const Base:ClassDecorator = (target)=>{
// 	target.prototype.heming  = "鹤鸣"
// 	target.prototype.fn = () =>{
// 		console.log('装饰器')
// 	}
// }// 如果用户要传参数可以使用(闭包 或者 函数柯里化 或者 工厂函数)
const Base = (name:string) => {const fn: ClassDecorator = (target) => {target.prototype.heming = nametarget.prototype.fn = () => {console.log('装饰器')}}return fn
}@Base('鹤鸣')
class Http {}const http = new Http() as any
console.log(http.heming)// 或者怕不兼容可以
class Http{}const http = new Http() as any
Base(Http)
http.fn()

方法装饰器 MethodDecorator 

/*** 方法装饰器 MethodDecorator 接受三个参数* @param target 原型对象 不再是构造函数* @param key 方法的名字* @param descriptor PropertyDescriptor 描述符* */
const Get = (url:string) => {const fn:MethodDecorator = (target:any, key , descriptor:PropertyDescriptor) => {axios.get(url).then((res) => {descriptor.value(res.data)})}return fn
}const Post = (url:string) => {const fn:MethodDecorator = (target:any,key, descriptor:PropertyDescriptor) => {axios.post(url).then((res)=>{descriptor.value(res.data)})}
}@Base('鹤鸣')
class Http {@Get('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')getList(@Result() data: any) {console.log(data.result.list, 'data')}@Post('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')create() {}
}

3.参数装饰器


/*** 参数装饰器 ParameterDecorator* @param  target 原型对象* @param  key 方法名* @param index 数据所在的位置* @param  reflect-metadata  数据的反射* */const Result = () => {const fn: ParameterDecorator = (target, key, index) => {Reflect.defineMetadata('key', 'result', target)}return fn
}@Base('鹤鸣')
class Http {@Nameheming: stringconstructor() {this.heming = '鹤鸣'}@Get('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')getList(@Result() data: any) {console.log(data, 'data')}// @Post('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')create() {}
}const http = new Http() as any

综合代码

// 1.类装饰器 ClassDecorator  target 返回的是一个构造函数
//2.属性装饰器 PropertyDecorator
//3.参数装饰器 ParameterDecorator
// 4.方法装饰器 MethodDecorator PropertyDescriptor
// 5.装饰器工长
// 6. import 'reflect-metadata'
// 7.axios
import axios from 'axios';
import 'reflect-metadata'/*** 类装饰器  ClassDecorator* @param target  形参  target 是形参,可以是任何名字* @param  result  返回结果:构造函数* @param name* */
// const Base:ClassDecorator = (target)=>{
// 	target.prototype.heming  = "鹤鸣"
// 	target.prototype.fn = () =>{
// 		console.log('装饰器')
// 	}
// }// 如果用户要传参数可以使用(闭包 或者 函数柯里化 或者 工厂函数)
const Base = (name: string) => {const fn: ClassDecorator = (target) => {target.prototype.heming = nametarget.prototype.fn = () => {// console.log('装饰器')}}return fn
}/*** 方法装饰器 MethodDecorator 接受三个参数* @param target 原型对象 不再是构造函数* @param key 方法的名字* @param descriptor PropertyDescriptor 描述符* */
const Get = (url: string) => {const fn: MethodDecorator = (target: any, _key: any, descriptor: PropertyDescriptor) => {let key = Reflect.getMetadata('key', target)axios.get(url).then((res) => {descriptor.value(key ? res.data[key] : res.data)})}return fn
}// const Post = (url:string) => {
// 	const fn:MethodDecorator = (target:any,key, descriptor:PropertyDescriptor) => {
// 		axios.post(url).then((res)=>{
// 			descriptor.value(res.data)
// 		})
// 	}
// }/*** 参数装饰器 ParameterDecorator* @param  target 原型对象* @param  key 方法名* @param index 数据所在的位置* @param  reflect-metadata  数据的反射* */const Result = () => {const fn: ParameterDecorator = (target, key, index) => {Reflect.defineMetadata('key', 'result', target)}return fn
}/*** 属性装饰器 PropertyDecorator* @param target 原型对象* @param key 属性* */
const Name: PropertyDecorator = (target, key) => {console.log(target, key)
}@Base('鹤鸣')
class Http {@Nameheming: stringconstructor() {this.heming = '鹤鸣'}@Get('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')getList(@Result() data: any) {console.log(data, 'data')}// @Post('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')create() {}
}const http = new Http() as any
// console.log(http.heming)// 或者怕不兼容可以
// class Http{
//
// }
//
// const http = new Http() as any
// Base(Http)
// http.fn()

http://www.lryc.cn/news/323098.html

相关文章:

  • 第十四章 TypeScript tsconfig.json配置文件
  • 科技助力高质量发展:新质生产力的崛起与企业数字化转型
  • Redis - 缓存访问 缓存穿透 缓存击穿 缓存雪崩
  • SAP Business Application Studio(BAS)中开发Fiori App的基础知识
  • DashScope - 阿里模型服务灵积
  • 个人信息-求职[web前端]
  • Apache DolphinScheduler 社区开启讲师招募,赶快加入吧!
  • 【HTML面试题】src和href的区别
  • 电脑文件msvcp100.dll丢失原因,如何快速修复msvcp100.dll
  • 安装OneNote for Win10 | Win10/Win11
  • 力扣242. 有效的字母异位词
  • windows server 下的mysql 8.0.28修改数据库目录
  • 【Excel自动化办公】使用openpyxl对Excel进行读写操作
  • 大龄女程序员脱单指南:如何科学评估你的Mr. Right?(含C语言代码示例)
  • 深入剖析Java并发库(JUC)之StampedLock的应用与原理
  • 【PMP】每日一练2
  • 2024年投影仪显示技术怎么选?哪个好?优缺点详解,买前必看
  • Git Bash命令初始化本地仓库,提交到远程仓库
  • Docker 学习笔记一
  • Git一点通
  • 商标转让有哪些好处 商标转让条件 商标转让流程
  • 诺视科技完成亿元Pre-A2轮融资,加速Micro-LED微显示芯片商业化落地
  • Unity定时播放音乐
  • 如何做接口测试?
  • U盘打不开提示格式化怎么办,U盘提示格式化数据恢复
  • LeetCode - 存在重复元素
  • RUST egui体验
  • 详解llamaindex
  • 管理类联考–复试–英文面试–问题--规划介绍原因做法--汇总
  • 成都百洲文化传媒有限公司电商新浪潮的领航者