1

我想使用从同一类的方法装饰器传递给类装饰器的信息。这是(虚拟)类的样子:

@classDecorator({
  something: 'very interesting'
})
class MyClass{
  @methodDecorator({
    pure: false
  })
  someMethod() {
    ...
  }
}

现在我有兴趣在 methodDecorator 中使用给 classDecorator ( {something: very interesting'}) 的参数。

我希望我可以使用 Reflect API,但无济于事:

function classDecorator(info) {
  // classDecorator parameter available here as 'info'

  return function(target, propertyKey, descriptor) {
    return descriptor
  }
}

function methodDecorator(config) {
  // propertyDecorator parameter availabe here as 'config'
  return function(target, propertyKey, descriptor) {

    // I thought I could access the class' decorator params with the reflect api
    Reflect.getMetadata('custom:annotation', target)
    Reflect.getMetadata('design:type', target)
    Reflect.getMetadata('design:paramtypes', target)
    // but all of the above are undefined

    return descriptor
  }

}

是否可以从同一类的 methodDecorator 中访问 c​​lassDecorator 的参数?

4

0 回答 0