我是 Angular 的新手,似乎已经为一些看起来应该很简单的事情陷入了困境。我在使用 ngx-bootstrap 模式对话框时遇到问题。我正在使用 ModalContentComponent 模式。
export class ModalContentComponent implements OnInit {
title: string;
constructor(public bsModalRef: BsModalRef, private myService: MyService) {}
ngOnInit() {
...
}
onSubmit(form: NgForm){
...
this.myService.sort();
this.bsModalRef.hide();
}
我有一个组件,它打开注入我的服务和 BSModalService 之一的模式,如下所示:
constructor(private myService: MyService, public modalService: BsModalService ) {}
我使用以下内容从此组件打开模态:
openModalWithComponent() {
const initialState = {
title: 'Preferences';
...
};
this.bsModalRef = this.modalService.show(ModalContentComponent,{initialState});
}
我导入 ModalContentComponent 并将其添加到我的 app.module.ts 中的声明数组中。我也将我的服务仅在此级别标识为提供者。
import { ModalContentComponent } from '.my.component';
import { MyComponent } from '.my.component';
import { MyService } from '.my.service';
declarations: [
MyComponent,
ModalContentComponent,
],
providers: [MyService],
为了让模态正常工作,我必须将 ModalContentComponent 添加到 app.module.ts entryComponents 数组中。一旦我这样做了,模式就会出现,一切似乎都正常。
entryComponents: [
ModalContentComponent
]
但是,myService 服务并未被视为 Singleton。ModalContentComponent 正在获取自己的 MyService 服务实例。
如何让 ModalContentComponent 使用 myService 的单个共享实例?
我认为 entryComponents 包含导致附加服务实例出现问题,但没有它我无法让模式工作。
欢迎任何指点。