Angular 2 how to extend class which has dependency injections

喜你入骨 提交于 2021-01-29 14:44:19

问题


i have the following class which i want to extend.

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
    )  
}

So i create the following class:

export class EnhancedComponent extends AngularComponent{
    constructor(private r: Renderer2, private editorService: AngularEditorService){
        super(r,editorService, ...)
    }
}

However i don't know how to pass the other dependencies

@Inject(DOCUMENT) private doc: any,
@Attribute("tabindex") defaultTabIndex: string,
@Attribute("autofocus") private autoFocus: any

Thanks for your help


回答1:


This should probably do the trick:
https://devblogs.microsoft.com/premier-developer/angular-how-to-simplify-components-with-typescript-inheritance/

Basically you use the Injector class to retrieve your injections instead of constructor injection.



来源:https://stackoverflow.com/questions/59659868/angular-2-how-to-extend-class-which-has-dependency-injections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!