【发布时间】:2019-05-27 11:28:37
【问题描述】:
我正在尝试创建一个可重用的 Modal 组件。 在 ModalService 中,我有一个主题,以及一个在主题上调用 next() 的方法。 ModalComponent 订阅了该主题,但无论何时调用服务中的方法,观察者的下一个函数都会触发两次。 有人知道这是什么原因吗?
export class ModalService {
openModal = new Subject();
constructor() { }
open(cmp) {
this.openModal.next(cmp);
}
}
模态组件:
export class ModalComponent implements OnInit {
component: ComponentRef<any>;
@ViewChild('entry', { read: ViewContainerRef }) entry: ViewContainerRef;
constructor(
private resolver: ComponentFactoryResolver,
private modalService: ModalService
) {}
ngOnInit() {
this.modalService.openModal.subscribe(cmp => {
// CALLD TWICE EVRY TIME THE SERVICE CALLS .next()
console.log(cmp);
});
}
【问题讨论】:
标签: javascript angular rxjs rxjs6 subject