有没有办法在不需要调用 super() 的情况下使用新的 Injection class 扩展子组件的构造函数?
Is there a way to extend a child component's constructor with a new Injection class without the need to call super()?
在 Angular 中,我有从父组件继承的子组件。这个父组件注入了多个 classes。我想用一个注入 class 来扩展子组件,我不在父组件 class 中使用它。在此扩展之前,不需要实例化构造函数和调用 super( *args* )
。但是,当我尝试扩展构造函数时,出现以下错误消息:
Constructors for derived classes must contain a 'super' call
有没有办法在通过注入扩展 class 时不需要调用超级 class?让我知道这个问题是否有不清楚的地方。
Parent component
@Component({ template: '' })
export abstract class ParentComponent<T> implements OnInit, OnDestroy {
constructor(
protected formBuilder: FormBuilder,
protected route: ActivatedRoute,
protected snackBar: MatSnackBar,
protected location: Location
) {
const routeParams = this.route.snapshot.params;
if (routeParams && routeParams.id) {
this.subjectId = this.route.snapshot.params.id;
}
}
}
Child component
export class ChildComponent extends ParentComponent<MyT> {
constructor(
/** I want to extend with the following service */
protected myService: Service
) {
// But as I instantiate this constructor, I also need to call super() with the required params
}
}
问题扩展
扩展我的问题;我不确定这种双重导入 super class 参数并传递它是否是开销。出现这个问题的主要原因是因为我尽量保持代码干净,尽量克服重复代码。为了在 super
调用中提供它而重复导入注入 classes 感觉有点无用。
我想你是害怕把所有的东西都注入到你身上parent。这就是为什么你需要这种行为。 angular DI 恐怕没有这样的选项。你能做的最好的事情是所有组件的通用样式注入 Injector
而不是它的依赖项,然后通过注入器获取所有依赖项。
class Parent {
private myService = this.injector.get(MyService)
private myService2 = this.injector.get(MyService2)
constructor(@Inject(INJECTOR) injector: Injector){}
}
class Child extends Parent {
constructor(@Inject(INJECTOR) injector: Injector) {
super(injector);
}
}
如果 Parent 不是所有这些情况下的组件,您可以在其构造函数中省略 @Inject(INJECTOR)
看了你的问题几遍后,我不确定这个是否理解,所以我会post它作为答案。
您可以扩展 parent class 并添加服务,但您仍然需要调用 parent 的构造函数。这是不可避免的。你不需要 re-inject 任何东西,尽管你至少仍然需要声明它们。
您的 child 的构造函数应如下所示:
constructor(
formBuilder: FormBuilder,
route: ActivatedRoute,
snackBar: MatSnackBar,
location: Location,
protected myService: Service // note the "protected" here but not above
) {
super(formBuilder, route, snackBar, location);
}
child 构造函数的非 protected
参数本质上是 "pass-throughs",可以这么说,您的 parent 声明应该注入什么。没有什么是 double-injected 或类似的东西。
在 Angular 中,我有从父组件继承的子组件。这个父组件注入了多个 classes。我想用一个注入 class 来扩展子组件,我不在父组件 class 中使用它。在此扩展之前,不需要实例化构造函数和调用 super( *args* )
。但是,当我尝试扩展构造函数时,出现以下错误消息:
Constructors for derived classes must contain a 'super' call
有没有办法在通过注入扩展 class 时不需要调用超级 class?让我知道这个问题是否有不清楚的地方。
Parent component
@Component({ template: '' })
export abstract class ParentComponent<T> implements OnInit, OnDestroy {
constructor(
protected formBuilder: FormBuilder,
protected route: ActivatedRoute,
protected snackBar: MatSnackBar,
protected location: Location
) {
const routeParams = this.route.snapshot.params;
if (routeParams && routeParams.id) {
this.subjectId = this.route.snapshot.params.id;
}
}
}
Child component
export class ChildComponent extends ParentComponent<MyT> {
constructor(
/** I want to extend with the following service */
protected myService: Service
) {
// But as I instantiate this constructor, I also need to call super() with the required params
}
}
问题扩展
扩展我的问题;我不确定这种双重导入 super class 参数并传递它是否是开销。出现这个问题的主要原因是因为我尽量保持代码干净,尽量克服重复代码。为了在 super
调用中提供它而重复导入注入 classes 感觉有点无用。
我想你是害怕把所有的东西都注入到你身上parent。这就是为什么你需要这种行为。 angular DI 恐怕没有这样的选项。你能做的最好的事情是所有组件的通用样式注入 Injector
而不是它的依赖项,然后通过注入器获取所有依赖项。
class Parent {
private myService = this.injector.get(MyService)
private myService2 = this.injector.get(MyService2)
constructor(@Inject(INJECTOR) injector: Injector){}
}
class Child extends Parent {
constructor(@Inject(INJECTOR) injector: Injector) {
super(injector);
}
}
如果 Parent 不是所有这些情况下的组件,您可以在其构造函数中省略 @Inject(INJECTOR)
看了你的问题几遍后,我不确定这个是否理解,所以我会post它作为答案。
您可以扩展 parent class 并添加服务,但您仍然需要调用 parent 的构造函数。这是不可避免的。你不需要 re-inject 任何东西,尽管你至少仍然需要声明它们。
您的 child 的构造函数应如下所示:
constructor(
formBuilder: FormBuilder,
route: ActivatedRoute,
snackBar: MatSnackBar,
location: Location,
protected myService: Service // note the "protected" here but not above
) {
super(formBuilder, route, snackBar, location);
}
child 构造函数的非 protected
参数本质上是 "pass-throughs",可以这么说,您的 parent 声明应该注入什么。没有什么是 double-injected 或类似的东西。