继承自抽象类具有依赖注入的问题

时间:2018-05-15 08:21:34

标签: angular inheritance angular-cli

我有一个父抽象类,声明如下:

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

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

当我运行ng serve --aot --preserve-symlinks时,一切正常。但是,每当我更新任何文件时,热重新加载都会更新构建的应用程序,并引发错误。

  

D中的Class BaseComponent:/some/path/components/base-component.ts从另一个编译单元中的Injectable扩展而不复制装饰器     请在课堂上添加Injectable或Pipe或Directive或Component或NgModule装饰器。

以下是我使用的版本:

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

知道如何让我的继承工作吗?

1 个答案:

答案 0 :(得分:1)

本地化是@Injectable,因此BaseComponent(扩展本地化)需要@Injectable装饰器。错误非常自我解释