装饰器分类装饰器、方法装饰器、参数装饰器、属性装饰器、访问器装饰器。不同装饰器所接收的参数也有所不同。
装饰器可以放在工具类,用到的 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()
}
}
}