在Angular 2上发送组件之前更改注释 [英] Change Annotations before sending component on Angular 2
本文介绍了在Angular 2上发送组件之前更改注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Angular 2上开发一个应用程序,在这个应用程序中,我需要动态更改@Component装饰器中的某些属性.这是我的代码:
I'm developping an app on Angular 2 and in this app I need to change dynamically some attributes inside @Component decorator. Here is my code :
function componentFactory (directives: any[], template: string) {
let annotations = Reflect.getMetadata('annotations', ComponentBase)
annotations[0].directives = directives
annotations[0].template = template
let metadata = new ComponentMetadata(annotations)
Reflect.defineMetadata('annotations', [ metadata ], ComponentBase)
return ComponentBase
}
问题是,当我将其与ComponentResolver一起使用时,出现此错误:组件'ComponentBase'必须设置为'template'或'templateUrl'."
The problem is that when I use it (with a ComponentResolver) I get this error : "Component 'ComponentBase' must have either 'template' or 'templateUrl' set.".
您有什么建议吗?
谢谢:)
推荐答案
我终于找到了创建动态组件的方法
I've finally found how to create dynamic components
function createDynamicComponent<T extends IBase> (componentAttributes: any, SuperClass: IConstructor<T>) {
class DynamicComponent extends (<IConstructor<IBase>> SuperClass) {}
return Component(componentAttributes)(DynamicComponent)
}
interface IConstructor<T> {
new (...args): T
}
interface IBase {}
我忘了我们可以将装饰器用作函数;)
I forgot that we can use decorators as functions ;)
这篇关于在Angular 2上发送组件之前更改注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文