[英]Extending angular2 component decorator
With angular 2.0.0-beta.8 I've created a couple of custom decorator that extends the @Component decorator. 使用angular 2.0.0-beta.8,我创建了几个自定义装饰器,它们扩展了@Component装饰器。
To make that I've used this code: 为此,我使用了以下代码:
import {..., View } from 'angular2/core';
...
export var MyCustomComponent:ComponentFactory =
<ComponentFactory>makeDecorator(MyCustomComponentMetadata, (fn:any) => fn.View = View);
Now, with angular 2.0.0-beta.12, the 'View' decorator has been dropped so the import throw an error because 'angular2/core' has no exported member 'View'. 现在,在angular 2.0.0-beta.12中,已删除“视图”装饰器,因此导入会引发错误,因为“ angular2 / core”没有导出成员“ View”。
How I'm supposed to create a custom component decorator? 我应该如何创建自定义组件装饰器?
You can do 你可以做
import {Component} from 'angular2/angular2';
import {MyCustomComponentMetadata} from ...;
export function MyCustomComponent(config: MyCustomComponentMetadata) {
return function(cls) {
var annotations = Reflect.getMetadata('annotations', cls) || [];
annotations.push(new Component(config));
Reflect.defineMetadata('annotations', annotations, cls);
return cls;
};
};
Angular 6 no more uses Reflection to define metadata for components. Angular 6不再使用反射来定义组件的元数据。 To override the built-in component decorator you could do something like below,
要覆盖内置的组件装饰器,您可以执行以下操作,
import { Component } from '@angular/core';
import componentRegistry from './ComponentRegistry';
function MyComponent(args: any = {}): (cls: any) => any {
const ngCompDecorator = Component(args);
return function (compType: any) {
ngCompDecorator(compType);
args.type && componentRegistry.register(args.type, compType, args.model);
}
}
export default MyComponent;
They metadata is stored in a static property called __annotations__
in the component's constructor. 它们的元数据存储在组件构造函数中的
__annotations__
静态属性中。 To know more about this please check here 要了解更多信息,请在这里查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.