请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

关于getName参数传递

老师,请问在调用test.getName()方法时,在装饰器中通过fn()方法执行。
如果test.getName()方法传递了参数,在装饰器中怎么将参数传入fn()执行呢

正在回答 回答被采纳积分+3

2回答

Dell 2020-03-15 20:47:49

你用arguments不就可以了嘛

0 回复 有任何疑惑可以回复我~
小蜜蜂15 2020-03-15 11:46:33

这样写也不行...等老师解答吧...

function catchError(msg: string) {
  return function (target: any, key: string, descriptor: PropertyDescriptor) {
    const fn = descriptor.value
    descriptor.value = function () {
      try {
        fn(msg)
      } catch (error) {
        console.log(msg)
      }
    }
  }
}
const userInfo: any = undefined
class Test {
  @catchError(msg) // msg报错
  getName(msg: string) {
    return msg
  }
}
const test = new Test()
test.getName('student')


0 回复 有任何疑惑可以回复我~
  • descriptor.value = function (...args: any[]) {
          try {
            fn.apply(this, args)
          } catch (error) {
            console.log(msg)
          }
        }
    回复 有任何疑惑可以回复我~ 2021-05-30 20:23:38
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号