Javascript 依赖项注入尝试为Angular 8中的分层抽象组件传递非服务
我有一个模态的角分量,这样模态的内容可以像这样传入: SomeComponent.html:Javascript 依赖项注入尝试为Angular 8中的分层抽象组件传递非服务,javascript,angular,typescript,dependency-injection,abstract-class,Javascript,Angular,Typescript,Dependency Injection,Abstract Class,我有一个模态的角分量,这样模态的内容可以像这样传入: SomeComponent.html: <my-modal-component> <div> all my content for the modal of SomeComponent goes here!</div> </my-modal-component> MyModalComponent.ts是一个模式组件,它接收内容,但也有自己的内容。它包含一些共享模式功能,如关闭模式。它
<my-modal-component>
<div> all my content for the modal of SomeComponent goes here!</div>
</my-modal-component>
MyModalComponent.ts是一个模式组件,它接收内容,但也有自己的内容。它包含一些共享模式功能,如关闭模式。它扩展了MyAbstractComponent,因为它也是一个组件
@Component({...}) // this is a real component with a view and module, so it's tagged as such
export class MyModalComponent extends MyAbstractComponent{
public constructor(
protected activeModal: NgbActiveModal,
protected logger: LoggerService,
component: IComponent, // THIS IS MY PROBLEM
) {
super(logger, component);
}
...
}
MyModalModule.ts
@NgModule({
declarations: [ MyModalComponent],
imports: [
CommonModule,
NgbModule
],
providers: [
LoggerService
// is it possible to somehow put the IComponent here?
],
exports: [ MyModalComponent, ],
entryComponents: [ MyModalComponent, ]
})
export class MyModalModule{ }
MyAbstractComponent.ts是所有组件的抽象组件,包括日志错误、设置、拆卸等常见功能
/* Acts like a component, but is not flagged as a real component because it doesn't have a view or module*/
export abstract class MyAbstractComponent implements OnInit, OnDestroy {
public constructor(
protected logger: LoggerService,
protected component: IComponent, // content passed in the constructor because it's not real dependency injection of services
) {
this.logger= logger;
this.component = component;
}
...
}
我试图传递的信息是用于日志记录的信息,下面是组件的示例。MyModalComponent
:
export namespace Components {
export const MyModalComponent: IComponent = {
userFriendlyName: "My Modal Component",
...
};
}
正如您所看到的,我需要从顶层组件类(SomeComponent
)中获取一些组件信息,并将这些信息下放到MyAbstractComponent`。如果我只需要通过一个级别,我就可以通过超级构造函数传递组件信息。但是,由于此组件信息需要经过组件的第二层,其中只有服务可以作为组件中注入的一部分,因此我收到有关DI的以下错误:
中的错误无法解析MyModalComponent.ts中MyModalComponent的所有参数:([object object],[object object],?)。
其中,?
是非服务的组件信息
我可以通过不同的方式(通过
@Input
或使用getter/setter)传递所需的信息,但在一个完美的世界中,构造函数中的一切都会准备好。我相信其他人以前已经面对并解决过这个问题。有什么想法吗?什么是组件。MyModalComponent?是MyModalComponent的实例吗?请输入Components类的代码。@Rodrigo这只是一些信息,例如用于记录的组件名称。我已经包括了它的一个例子作为参考。请看我的编辑。
export namespace Components {
export const MyModalComponent: IComponent = {
userFriendlyName: "My Modal Component",
...
};
}