例如:

    1. const ErrorDecorator: MethodDecorator = (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
    2. const method = descriptor.value;
    3. try {
    4. method()
    5. } catch(err: any) {
    6. console.log(`%c自定义错误信息`, 'color: green;font-size: 30px');
    7. console.log(`%c${err.message}`, 'color: red;font-size:20px');
    8. }
    9. }
    10. class Player {
    11. @ErrorDecorator
    12. public run() {
    13. throw new Error('运行错误啦')
    14. }
    15. }
    16. new Player().run()