Angular 2如何扩展具有依赖项注入的类 [英] Angular 2 how to extend class which has dependency injections

查看:85
本文介绍了Angular 2如何扩展具有依赖项注入的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要扩展以下课程.

 导出类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:任何 

感谢您的帮助

解决方案

这大概可以解决问题:
https://devblogs.microsoft/

基本上,您使用 Injector 类来检索注入,而不是构造函数注入.

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

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.

这篇关于Angular 2如何扩展具有依赖项注入的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆