angular - 从抽象类继承存在依赖注入(inject)问题

标签 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时,一切正常。但是,每当我更新任何文件时,热重载都会更新构建的应用程序,并引发错误。

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.

以下是我使用的版本:

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

知道如何让我的继承发挥作用吗?

最佳答案

Localization 是 @Injectable,因此 BaseComponent(扩展 Localization)需要 @Injectable 装饰器。该错误非常不言自明

关于angular - 从抽象类继承存在依赖注入(inject)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50345499/

相关文章:

angular - 在 typescript Angular 应用程序中解析模块时出现 404

c++ - VS2008 C++好像不能继承const重载方法

c++ - 我应该如何在 C++ 中表示继承对象树?

angular - 在 Angular 7+ 项目中同时使用 Sass (Scss) 和 Less

Angular-CLI AOT - 错误堆栈跟踪

css - Angular 2 : Add elements to dom programmatically with viewencaptulation

javascript - 将桌面 View 中的模态弹出窗口转换为移动 View 中的新页面的任何想法

objective-c - 在 Objective-C 中,我是否可以重写父类用来遵守协议(protocol)的子类中的方法?

angular - 模块构建失败 : SyntaxError: 'import' and 'export' may appear only with 'sourceType: "module"'

angular - 以 Angular 8 向服务器数据添加查看更多选项