Rxjs 订阅触发不止一次
Rxjs Subscription triggers more than once
我的订阅被触发了不止一次,这非常神秘 - 我不知道我的错误是什么 - 我在另一个人身上采用了相同的方法,但它并没有被触发超过一次
@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
但在第一个观察者休息后,所有订阅都加倍并在我的控制台中记录两次 - 请帮助我修复它
只要你对所有事情都使用 Subject,就使用 Behavior Subject!我前段时间也遇到过这个问题,但这对我有用!
bindPatientId: Subject<number> = new BehaviourSubject<number>();
bindTabId: Subject<number> = new BehaviourSubject<number>();
订阅后,returns 主题的最后一个值。常规可观察对象仅在收到 onnext 时触发
你也可以使用
import { take } from 'rxjs/operators';
然后添加
.pipe(
take(1)
订阅前
看看这里
https://www.learnrxjs.io/operators/filtering/take.html
希望对您有所帮助!
这可能是因为您的组件正在被销毁并重新创建。因此,当您的组件销毁时,您必须销毁所有订阅。您可以在 ngOnDestroy
生命周期方法
中执行此操作
代码:
class MyComponent implements OnInit, OnDestroy {
private sub: Subscription;
ngOnInit() {
this.sub = this._common.getpatientId.subscribe(
(res) => {
this.patientID = res;
console.log("console inside observable", this.patientID);
});
}
ngOnDestroy(){
this.sub.unsubscribe():
}
我的订阅被触发了不止一次,这非常神秘 - 我不知道我的错误是什么 - 我在另一个人身上采用了相同的方法,但它并没有被触发超过一次
@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
但在第一个观察者休息后,所有订阅都加倍并在我的控制台中记录两次 - 请帮助我修复它
只要你对所有事情都使用 Subject,就使用 Behavior Subject!我前段时间也遇到过这个问题,但这对我有用!
bindPatientId: Subject<number> = new BehaviourSubject<number>();
bindTabId: Subject<number> = new BehaviourSubject<number>();
订阅后,returns 主题的最后一个值。常规可观察对象仅在收到 onnext 时触发 你也可以使用
import { take } from 'rxjs/operators';
然后添加
.pipe(
take(1)
订阅前
看看这里 https://www.learnrxjs.io/operators/filtering/take.html
希望对您有所帮助!
这可能是因为您的组件正在被销毁并重新创建。因此,当您的组件销毁时,您必须销毁所有订阅。您可以在 ngOnDestroy
生命周期方法
代码:
class MyComponent implements OnInit, OnDestroy {
private sub: Subscription;
ngOnInit() {
this.sub = this._common.getpatientId.subscribe(
(res) => {
this.patientID = res;
console.log("console inside observable", this.patientID);
});
}
ngOnDestroy(){
this.sub.unsubscribe():
}