public class ModelService{
subject = new Subject();
raiseOk(): void {
this.subject.next({action: 'SUBMIT ... });
}
onOk(): Observable<any> {
return this.subject.asObservable().pipe(
filter(x => x.action=='SUBMIT'),
map(x => x));
}
}
// Our modal dialog will invoke raiseOk() event when ok is clicked.
// Assuming ModelService registered and injected into component
export class Component{
ngOnInit(): void{
this.modelService.onOk().subscribe(x=> {
console.log('ok clicked');
});
}
}
如果我们现在切换,我们有3个观测值,如果我们现在切换,我们有4个观测值。处理程序根据路由切换触发3次、4次及更多。
我很困惑,因为
this.http.get()
angular的服务也有subscribe,但它不会像上面那样触发多次。我们遗漏了什么吗?非常感谢您的帮助。