ts装饰器保存this指向

要使用 function 而不是 箭头函数

typescript 复制代码
interface MethodDecorator<T> {
  (
    target: Object,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<T>
  ): TypedPropertyDescriptor<T> | void;
}

type TMethodDecorator = MethodDecorator<any>;

const matchImport: TMethodDecorator = (target, propertyKey, descriptor) => {
  const callback = descriptor.value;

  descriptor.value = function (...args: any[]) {
    console.log("decorator", this);
    return callback.call(this, ...args);
  };
};
相关推荐
kongba00741 分钟前
项目打包 Python Flask 项目发布与打包专家 提示词V1.0
开发语言·python·flask
froginwe111 小时前
C 语言测验
开发语言
今夕资源网1 小时前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码
机器视觉知识推荐、就业指导2 小时前
Qt:真正的门槛不是入门,而是维护
开发语言·qt
hhb_6182 小时前
Dylan 语言核心特性与工程实践深度解析
开发语言·c#
无巧不成书02182 小时前
零基础Java网络编程全解:从核心概念到Socket实战,一文打通Java网络通信
java·开发语言·网络
MXN_小南学前端2 小时前
watch详解:与computed 对比以及 Vue2 / Vue3 区别
前端·javascript·vue.js
饭小猿人3 小时前
Flutter实现底部动画弹窗有两种方式
开发语言·前端·flutter
aq55356003 小时前
Workstation神技:一键克隆调试环境
java·开发语言
lly2024063 小时前
框架:构建高效系统的基石
开发语言