【问题标题】:Angular 2 how to extend class which has dependency injectionsAngular 2如何扩展具有依赖注入的类
【发布时间】:2020-04-26 19:35:35
【问题描述】:

我有以下要扩展的类。

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

感谢您的帮助

【问题讨论】:

    标签: angular oop dependencies extends


    【解决方案1】:

    这应该可以解决问题:
    https://devblogs.microsoft.com/premier-developer/angular-how-to-simplify-components-with-typescript-inheritance/

    基本上你使用Injector 类来检索你的注入而不是构造函数注入。

    【讨论】:

    • 谢谢,但在我的情况下我该怎么做。就我而言,我无法修改基类。它来自图书馆...
    • 如果你不能修改基类,你可能需要将它包装到你自己的 BaseComponent 中,它会实例化 lib 组件。
    猜你喜欢
    • 2016-06-22
    • 2017-01-05
    • 2014-06-05
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多