简体   繁体   English

在Angular 2+中继承组件

[英]Inheriting components in Angular 2+

Is it OK to inherit @Component classes in Angular 2+ ? 可以在Angular 2+中继承@Component类吗? Something like this (note that the constructors will inject a service): 这样的事情(请注意,构造函数将注入服务):

@Component({})
export class Parent {
    constructor ( s0: Service1 ) {}
}

@Component({
     template: 'text1'
})
export class Child1 extends Parent {
    constructor ( s1: Service1 ) { super(s1); }
}

@Component({
     template: 'text2'
})
export class Child2 extends Parent {
    constructor ( s2: Service1 ) { super(s2); }
}

Sure although you might want to mark type Parent as abstract and remove @Component as the decorator on that type as well. 当然可以,尽管您可能希望将Parent类型标记为abstract并同时删除@Component作为该类型的装饰器。

export abstract class Parent {
    constructor (protected s0: Service1 ) {}
}

I also marked s0 as protected so it is accessible in derived types 我还将s0标记为受保护,因此可以在派生类型中对其进行访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM