我在Angular 2上开发应用程序,在这个应用程序中,我需要动态更改@Component装饰器中的一些属性。这是我的代码:
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)时,我收到此错误:" Component' ComponentBase'必须有'模板'或者' templateUrl'设定"
你有什么建议吗?
谢谢:)
答案 0 :(得分:0)
我终于找到了如何创建动态组件
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 {}
我忘了我们可以使用装饰器作为函数;)