装饰器分类装饰器方法装饰器参数装饰器属性装饰器访问器装饰器。不同装饰器所接收的参数也有所不同。

TS 装饰器 + 实践应用

TS 装饰器介绍

case

NestJs

装饰器可以放在工具类,用到的 service 使用原来调用位置的注入

分布式定时任务执行注解——TS 装饰器 nestjs/schedule nestjs定时任务

前端

import { MessageBox } from 'element-ui'
/**
 * 装饰器:确认弹窗
 */
export const confirm = (message) => {
  return (target, name, descriptor) => {
    const fn = descriptor.value
    descriptor.value = async (...rest) => {
      await MessageBox.confirm(message)
      fn.apply(this, rest)
    }
  }
}

// 使用装饰器
export default {
    methods: {
        @confirm('确认删除吗?')
        async onDelete() {
            await api.deleteUser()
        }
    }
}