加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

访问类中的元注释(TypeScript)

发布时间:2020-12-17 09:57:22 所属栏目:安全 来源:网络整理
导读:当我用TypeScript中的元数据来注释类时,例如要创建一个Angular2组件,我可以访问该类中的元数据吗? import {Component} from 'angular2/core';@Component({ selector: 'app',templateUrl: '/app/components/app/app.component.html'})export class AppCompon
当我用TypeScript中的元数据来注释类时,例如要创建一个Angular2组件,我可以访问该类中的元数据吗?
import {Component} from 'angular2/core';

@Component({
    selector: 'app',templateUrl: '/app/components/app/app.component.html'
})
export class AppComponent {

    // can I access 'templateUrl' from above annotation here?

}
您可以将注释/装饰器看作正常的函数调用.对于这个功能,’Class / Function’对象(不是实例)在第一个参数中获取发送,在第二个参数中获取参数(元数据).

然而,如果某些东西被添加到类的原型(坏习惯/暴露属性)中,则取决于该函数的实现. TypeScript编译器和Angular2做不同的事情.

它们使用__decorate和__metadata函数,它们是由TypeScript编译器生成的.数据加入了Object.defineProperty()功能.负责这个的包是Reflect.(在引擎罩下使用Object.defineProperty()函数与WeakMap的组合).

函数Reflect.defineMetadata()用于设置注释,并获得明显的Reflect.getMetadata().

TLDR;

>要获得角色2中的类/组件的注释,您可以
使用:

Reflect.getMetadata('annotations',ComponentClass); //@Component({}),@Pipe({}),...

>要从angular2中的构造函数参数中获取注释,您必须使用:

Reflect.getMetadata('parameters',ComponentClass); //@Inject()

>要从角度2中的类中的属性获取注释,您可以
必须使用:

Reflect.getMetadata('propMetadata',ComponentClass); //@HostBinding(),@Input(),...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读