Angular 2如何扩展具有依赖项注入的类 [英] Angular 2 how to extend class which has dependency injections
问题描述
我要扩展以下课程.
导出类AngularComponent {构造函数(私人r:Renderer2,私人editorService:AngularEditorService,@Inject(DOCUMENT)私人doc:任何,私人消毒剂:DomSanitizer,私人cdRef:ChangeDetectorRef,@Attribute("tabindex")defaultTabIndex:字符串,@Attribute("autofocus")私人autoFocus:任何)}
所以我创建了以下类:
导出类EnhancedComponent扩展了AngularComponent {构造函数(私有r:Renderer2,私有editorService:AngularEditorService){超级(r,editorService,...)}}
但是我不知道如何传递其他依赖项
@Inject(DOCUMENT)私人doc:任意,@Attribute("tabindex")defaultTabIndex:字符串,@Attribute("autofocus")私人autoFocus:任何
感谢您的帮助
这大概可以解决问题: 基本上,您使用 i have the following class which i want to extend. So i create the following class: However i don't know how to pass the other dependencies Thanks for your help This should probably do the trick: Basically you use the 这篇关于Angular 2如何扩展具有依赖项注入的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
https://devblogs.microsoft/ Injector
类来检索注入,而不是构造函数注入.export class AngularComponent {
constructor(
private r: Renderer2,
private editorService: AngularEditorService,
@Inject(DOCUMENT) private doc: any,
private sanitizer: DomSanitizer,
private cdRef: ChangeDetectorRef,
@Attribute("tabindex") defaultTabIndex: string,
@Attribute("autofocus") private autoFocus: any
)
}
export class EnhancedComponent extends AngularComponent{
constructor(private r: Renderer2, private editorService: AngularEditorService){
super(r,editorService, ...)
}
}
@Inject(DOCUMENT) private doc: any,
@Attribute("tabindex") defaultTabIndex: string,
@Attribute("autofocus") private autoFocus: any
https://devblogs.microsoft.com/premier-developer/angular-how-to-simplify-components-with-typescript-inheritance/Injector
class to retrieve your injections instead of constructor injection.