【发布时间】:2019-02-15 18:18:50
【问题描述】:
我的订阅被多次触发非常神秘 - 我不知道我的错误是什么 - 我在另一个上采用了相同的方法,但它不会被触发多次
@Injectable({
providedIn: 'root'
})
export class CommonService {
constructor() {
this.getpatientId = this.bindPatientId.asObservable();
}
bindPatientId: Subject<number> = new Subject<number>();
getpatientId: Observable<number>;
setPatientId(patientId: number) {
this.bindPatientId.next(patientId);
}
bindTabId: Subject<number> = new Subject<number>();
}
为Subject设置新值
toggleProfile(patientId: number) {
this.commonService.setPatientId(patientId);
this.route.navigate(['/endrolPatient']);
}
从另一个组件监听
ngOnInit() {
this._common.getpatientId.subscribe(
(res) => {
this.patientID = res;
console.log("console inside observable", this.patientID);
});
}
在我的控制台中,当我通过 123 - 我得到 console inside observable 123 但在第一个观察者休息后,所有订阅都翻倍并在我的控制台中登录两次 - 请帮助我修复它
【问题讨论】:
-
after the first observer rest是什么意思?还请提供一些关于如何创建组件(从服务订阅 observable)的见解(代码) -
如果我向
subject添加新值,观察者会被多次触发值,但对于第一次更改它只会触发一次
标签: angular typescript rxjs angular-services