This is possible with inheritance. Different patterns of appearance and style, but (basically) the same basic component code. You still have to declare @Component metadata, so if you use parameter accessors, etc., you will also need to override them.
The first component:
import { Component, Input, OnInit } from '@angular/core'; import { KeyValuePairBase } from '../../model/key-value-pair-base' @Component({ moduleId: module.id, selector: 'select-list', templateUrl: './select-list.component.html', styleUrls: ['./select-list.component.scss'] }) export class SelectListComponent implements OnInit { @Input() private data: KeyValuePairBase[]; constructor() { super(); } ngOnInit() { if (!this.name) throw new Error("'name' property is required by 'select-list' component"); if (!this.friendlyName) throw new Error(`'friendlyName' property is required by 'select-list' component '${this.name}'`); } }
The second component:
import { Component } from '@angular/core'; import { SelectListComponent } from '../select-list/select-list.component' @Component({ moduleId: module.id, selector: 'radio-list', templateUrl: './radio-list.component.html', styleUrls: ['./radio-list.component.scss'] }) export class RadioListComponent extends SelectListComponent { }
source share