老师好,使用装饰器的时候报错了

来源:2-11 如何借助 TS 的高级类型 解决 2-9 类型自动提示问题

weixin_慕桂英5062843

2023-02-28

图片描述
图片描述图片描述视频中这样写没报错,我的代码中提示了错误,,

写回答

1回答

keviny79

2023-02-28


1.根据提供截图排查第一步:

tsconfig.json 是否设置 true

 "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,

    "emitDecoratorMetadata": true

2. 核对后把下面代码拷贝过去替换自己的代码:

import 'reflect-metadata'

export function reqProccess(methodType: string) {

  return function (reqPath: string) {

    return function (targetClassPrototype: any, methodname: string, methodDecri: PropertyDescriptor) {

      Reflect.defineMetadata('path', reqPath, targetClassPrototype, methodname)

      // console.log(Reflect.getMetadata('path', targetClassPrototype, methodname))

      Reflect.defineMetadata('methodType', methodType, targetClassPrototype, methodname)

      //console.log(Reflect.getMetadata('methodType', targetClassPrototype, methodname))

    }

  }

}


const get = reqProccess('get')

const post = reqProccess('post')

const put = reqProccess('put')

const del = reqProccess('delete')


export { get, post, put, del }


3.看看截图的方法提示是否能显示?

//img.mukewang.com/szimg/63fde7da09e2329113270687.jpg


如果以上3步弄好后还不行,就加入课程群,把项目发给我看下

0
1
weixin_慕桂英5062843
找到错误了,我晕 参数methodDecri的定义类写错
2023-03-01
共1条回复

前端高手养成计划-从前端到后端,全栈开发大型项目

从纯前端到“真正懂后端的前端”

215 学习 · 134 问题

查看课程