gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:05:23 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com