reflect-metadata Reflect.getMetadata() 方法不 return 提供的原型链上的元数据 object
reflect-metadata Reflect.getMetadata() method doesn't return metadata on the prototype chain of provided object
我正在使用 reflect-metadata 0.1.2。我有一个 parent class 作为 "MyCustom"。
export class MyCustom {}
我的 "Home" 组件 class 扩展了这个 "MyCustom" class。
@Component({
selector: 'home',
templateUrl: './app/components/home/home.html',
styleUrls: ['./app/components/home/home.css']
})
export class Home extends MyCustom {
}
我的目的是使用以下方法调用获取扩展 "MyCustom" class 的所有 classes 的元数据。
let annotations = Reflect.getMetadata('annotations', MyCustom);
对方法 Reflect.getMetadata() 的评论说,
Gets the metadata value for the provided metadata key on the target
object or its prototype chain.
然而我一无所获。如果我将 @Component 添加到 "MyCustom" class,如下所示,
@Component({
selector: 'hello'
})
export class MyCustom {}
我得到一个结果,即 "MyCustom" 上的注释。
为什么我在 subclasses 上得不到注释?非常感谢任何帮助。
元数据是为 Home
而不是为 MyCustom
保存的,并且继承在这里没有效果。
您可能会做的是拥有自己的装饰器,它将为父级添加元数据 class,然后使用所需的值调用 Component
装饰器。
类似于:
function MyComponent(props: any) {
return (ctor) => {
Reflect.defineMetadata('annotations', ctor.prototype.name, ctor.prototype);
return Component(props)(ctor);
}
}
@MyComponent({
selector: 'home',
templateUrl: './app/components/home/home.html',
styleUrls: ['./app/components/home/home.css']
})
class Home extends MyCustom {}
我没有测试过,之前也没有做过类似的事情,但应该可以。
你为什么不使用 "registry"?
有这样的东西:
const REGISTRY: { [name: string]: MyCustom } = {};
然后,为每个这样的组件注册它:
class Home extends MyCustom {
...
}
REGISTRY["home"] = Home;
注册表和组件不必在同一个文件中,只要您在需要的地方导入即可。
然后从您的主要组件很容易拥有所有这些组件。
我正在使用 reflect-metadata 0.1.2。我有一个 parent class 作为 "MyCustom"。
export class MyCustom {}
我的 "Home" 组件 class 扩展了这个 "MyCustom" class。
@Component({
selector: 'home',
templateUrl: './app/components/home/home.html',
styleUrls: ['./app/components/home/home.css']
})
export class Home extends MyCustom {
}
我的目的是使用以下方法调用获取扩展 "MyCustom" class 的所有 classes 的元数据。
let annotations = Reflect.getMetadata('annotations', MyCustom);
对方法 Reflect.getMetadata() 的评论说,
Gets the metadata value for the provided metadata key on the target object or its prototype chain.
然而我一无所获。如果我将 @Component 添加到 "MyCustom" class,如下所示,
@Component({
selector: 'hello'
})
export class MyCustom {}
我得到一个结果,即 "MyCustom" 上的注释。
为什么我在 subclasses 上得不到注释?非常感谢任何帮助。
元数据是为 Home
而不是为 MyCustom
保存的,并且继承在这里没有效果。
您可能会做的是拥有自己的装饰器,它将为父级添加元数据 class,然后使用所需的值调用 Component
装饰器。
类似于:
function MyComponent(props: any) {
return (ctor) => {
Reflect.defineMetadata('annotations', ctor.prototype.name, ctor.prototype);
return Component(props)(ctor);
}
}
@MyComponent({
selector: 'home',
templateUrl: './app/components/home/home.html',
styleUrls: ['./app/components/home/home.css']
})
class Home extends MyCustom {}
我没有测试过,之前也没有做过类似的事情,但应该可以。
你为什么不使用 "registry"?
有这样的东西:
const REGISTRY: { [name: string]: MyCustom } = {};
然后,为每个这样的组件注册它:
class Home extends MyCustom {
...
}
REGISTRY["home"] = Home;
注册表和组件不必在同一个文件中,只要您在需要的地方导入即可。
然后从您的主要组件很容易拥有所有这些组件。