angular 2 inheriting from a base-component

帅比萌擦擦* 提交于 2019-12-03 03:31:26
Thierry Templier

It's not possible the way you do since Angular2 will only have a look at the annotations for the current component but not on the component above.

That being said, you can work at the level of annotations to make inherit annotations of the parent component:

export function Inherit(annotation: any) {
  return function (target: Function) {
    var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
    var parentAnnotations = Reflect.getMetadata('design:paramtypes', parentTarget);

    Reflect.defineMetadata('design:paramtypes', parentAnnotations, target);
  }
}

And use it like this:

@Inherit()
@Component({
  (...)
})
export class ChildComponent1 extends BaseComponent {
  constructor() {
    super(arguments);
  }
}

See this question for more details:

The following article could interest you to understand what happens under the hood:

You also need to be aware that working on annotations directly has drawbacks especially regarding offline compilation and for component introspection in IDEs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!