组件的继承
<p>base works!</p>
<div>{{aa}}</div>
<div class="fl-div">{{bb}}</div>
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'l-base',
templateUrl: './base.component.html',
styleUrls: ['./base.component.less']
})
export class BaseComponent implements OnInit {
@Input() bb = 'SeriousLose';
public aa = 'aa';
constructor() { }
ngOnInit() {}
}
<p>other-base works!</p>
<l-base [bb]="bb"></l-base>
import { Component } from '@angular/core';
import { BaseComponent } from '../base/base.component';
@Component({
selector: 'l-other-base',
templateUrl: './other-base.component.html',
styleUrls: ['./other-base.component.less']
})
export class OtherBaseComponent extends BaseComponent {
aa = 'bb';
}
源码
import { HostListener } from '@angular/core';
export abstract class BaseComponent {
protected abstract getName(): string;
@HostListener('click')
onClick() {
alert('Click');
}
}
import { Component } from '@angular/core';
import { BaseComponent } from './base';
@Component({
selector: 'app-my',
templateUrl: './my.component.html',
styleUrls: ['./my.component.less'],
})
export class MyComponent extends BaseComponent {
protected getName(): string {
return 'MyComponent';
}
}