[英]Extending angular2 component decorator
使用angular 2.0.0-beta.8,我创建了几个自定义装饰器,它们扩展了@Component装饰器。
为此,我使用了以下代码:
import {..., View } from 'angular2/core';
...
export var MyCustomComponent:ComponentFactory =
<ComponentFactory>makeDecorator(MyCustomComponentMetadata, (fn:any) => fn.View = View);
现在,在angular 2.0.0-beta.12中,已删除“视图”装饰器,因此导入会引发错误,因为“ angular2 / core”没有导出成员“ View”。
我应该如何创建自定义组件装饰器?
你可以做
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不再使用反射来定义组件的元数据。 要覆盖内置的组件装饰器,您可以执行以下操作,
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;
它们的元数据存储在组件构造函数中的__annotations__
静态属性中。 要了解更多信息,请在这里查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.