简体   繁体   English

从抽象类继承存在依赖项注入问题

[英]Inheriting from an abstract class has issue with Dependency Injection

I have a parent abstract class declared like the following : 我有一个声明的父抽象类,如下所示:

import { Localization } from 'angular-l10n';
import { SomeModel} from '../../api/models/some-model';
import { SomeService } from '../../api/services/some.service';

export abstract class BaseComponent extends Localization implements OnInit {

  someModel: SomeModel[];

  constructor(
    protected someService: SomeService
  ) {
    super();
  }

  ngOnInit() {
    this.someService
      .getSomeModel()
      .pipe(take(1))
      .subscribe(ref => (this.someModel= ref), err => this.onError(err));
}

The Child class uses this parent like the following : Child类使用此父级,如下所示:

@Component({
  selector: 'some-child-component',
  templateUrl: './some-child.component.html',
  styleUrls: ['./some-child.component.scss'],
})
export class SomeChildComponent extends BaseComponent implements OnInit {

  constructor(
    protected someService: SomeService
  ) {
    super(someService)
  }


  ngOnInit() {
    super.ngOnInit();
  }
}

When I run ng serve --aot --preserve-symlinks , everything works fine. 当我运行ng serve --aot --preserve-symlinks ,一切正常。 However, whenever I update any file, a hot reload updates the builded application, and an error is thrown. 但是,每当我更新任何文件时,热重装都会更新生成的应用程序,并且会引发错误。

Class BaseComponent in D:/some/path/components/base-component.ts extends from a Injectable in another compilation unit without duplicating the decorator Please add a Injectable or Pipe or Directive or Component or NgModule decorator to the class. D:/some/path/components/base-component.ts中的BaseComponent类是从另一个编译单元中的Injectable扩展而来的,无需复制装饰器。请在该类中添加Injectable或Pipe或Directive或Component或NgModule装饰器。

Here are the versions I use : 这是我使用的版本:

Angular CLI: 1.7.4
Node: 6.11.4
OS: win32 x64
Angular: 5.2.10
typescript: 2.6.2
webpack: 3.11.0

Any idea how I could make my inheritance work? 知道如何使继承有效吗?

Localization is @Injectable, therefor BaseComponent (which extends Localization) needs @Injectable decorator. 本地化是@Injectable,因此BaseComponent(扩展了本地化)需要@Injectable装饰器。 The error is pretty self explanatory 错误是不言自明的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM