8
Doubt
I would like to create extensions for some components already implemented in Angular 2, without having to rewrite them almost completely, because the base component could undergo changes and I would like these changes to be reflected also in their derivative components.
Example
I created this simple example to try to expose my doubt better:
With the following base component app/base-panel.component.ts:
import {Component, Input} from 'angular2/core';
@Component({
selector: 'base-panel',
template: '<div class="panel" [style.background-color]="color" (click)="onClick($event)">{{content}}</div>',
styles: [`
.panel{
padding: 50px;
}
`]
})
export class BasePanelComponent {
@Input() content: string;
color: string = "red";
onClick(event){
console.log("Click color: " + this.color);
}
}
I would like to create another derivative component that would only change, for example, the behavior of the base component, in the case of the color example, app/my-panel.component.ts:
import {Component} from 'angular2/core';
import {BasePanelComponent} from './base-panel.component'
@Component({
selector: 'my-panel',
template: '<div class="panel" [style.background-color]="color" (click)="onClick($event)">{{content}}</div>',
styles: [`
.panel{
padding: 50px;
}
`]
})
export class MyPanelComponent extends BasePanelComponent{
constructor(){
this.color = "blue";
}
}
Full and functional example in Plunker.
Note: Obviously this example is simple and could be solved otherwise without needing to use inheritance, but it aims only to illustrate the real problem.
Problem
As you can see in the implementation of the derivative component app/my-panel.component.ts, much of the implementation was repeated, and the only part actually inherited was the class BasePanelComponent, but the @Component had to be basically repeated completely, and not only the parts changed, as o selector: 'my-panel'.
Question
The some way to make a literally complete inheritance of an Angular2 component, inheriting the definitions of class and marking/annotations, such as @Component?
Edit 1 - Resource Request
Feature request added to angular2 project on Github: Extend/Inherit angular2 Components Annotations #7968
Edit 2 - Closed Request
The request was closed, for this reason, that briefly would be not knowing how will be made the merge of Developer. Leaving us no options. So my opinion is is quoted in Issue, that in Portuguese would be something like:
"If Decorator/Annotation has these limitations, which according to your arguments can not be easily circumvented in the library, I ask you a question: Why do you use them? Wouldn’t it be a better option to ignore/discard them for development a library of angular2 size and complexity? Or want to invest time/money in a library with primary limitations like the one listed on this Issue? I believe that this is still the time for major changes that can alter the direction of angular2, because the library is still in RC1. It would be extremely disappointing to see a stable version of a library as angular2 being released with such a limitation.".
If I understood your doubt correctly you’d like to have one
ComponentChild (may have more than oneComponentChild within the sameComponentParent), let’s say, that you manage to change some property of yourComponentParent (1ComponentParent for manyComponentChild). Would that be?– celsomtrindade
@Celsomtrindade, is if I understood your question I think it’s almost that! Briefly, I would like the real inheritance effect for the components!
– Fernando Leal