RxJs BehaviorSubject 服务中的 next() 方法
RxJs BehaviorSubject's next() method in service
我使用以下方法在添加新记录后刷新 Details
页面。我也更新一条记录后需要刷新
EventProxyService
export class EventProxyService {
private eventTracker = new BehaviorSubject<any>();
getEvent(): Observable<any> {
return this.eventTracker.asObservable();
}
setEvent(param: any): void {
this.eventTracker.next(param);
}
}
创建组件:
export class CreateComponent implements OnInit {
constructor(private eventProxyService: EventProxyService) { }
create(param: any): void {
//other staff related to creating record
this.eventProxyService.setEvent(param);
}
}
详细信息组件:
export class DetailsComponent implements OnInit {
subscription;
constructor(private eventProxyService: EventProxyService) { }
ngOnInit() {
this.subscription = this.eventProxyService.getEvent().subscribe((param: any) => {
this.refresh(param);
);
}
refresh(param) {
this.record = param; //update record via new one passed from service
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}
该方法效果很好,但我对以下问题有点困惑:
1) 因为我也在 update()
方法中调用 this.eventProxyService.setEvent(param);
就像在 create()
方法中一样,调用这个是不是个好主意在我的服务级别而不是组件级别?
2) 上面 CreateComponent
中触发 next() 方法时有什么问题吗?
我个人认为你目前的做法是完全有效的。您可以考虑添加限制规则,就像您已经提到的那样,只从您的服务层调用 setEvent
。这将改善组件的单一职责。无论如何,我认为这真的取决于项目的规模和生命周期。对于开始一个项目,所以肯定会像你一样做!
我认为你的设计非常适合 BehaviourSubject
(至少在我看来是这样)。
我可以建议的一件事是在单例服务中尝试 BehaviourSubject
。 (在你的情况下我没注意到)
what if I call the setEvent() in the create() method of my service (not this proxyService, I meant other service where I keep my CRUD
methods)
如果我正确理解了你的问题,答案是否定的。
因为从另一个服务调用一个服务方法是不可取的,所以保持简单。在实际项目中,一旦执行了 CRUD 方法,我们将尝试借助服务方法(但不在服务中)从组件本身刷新数据。
I call the next() via setEvent() method in the CRUD methods, after create, update, delete operations are completed. Is it a good
approach, it is there a better way in real world examples e.g. calling
next() in the service (CRUD) as I asked in the first question
是的,这是一个很好的方法。
我使用以下方法在添加新记录后刷新 Details
页面。我也更新一条记录后需要刷新
EventProxyService
export class EventProxyService {
private eventTracker = new BehaviorSubject<any>();
getEvent(): Observable<any> {
return this.eventTracker.asObservable();
}
setEvent(param: any): void {
this.eventTracker.next(param);
}
}
创建组件:
export class CreateComponent implements OnInit {
constructor(private eventProxyService: EventProxyService) { }
create(param: any): void {
//other staff related to creating record
this.eventProxyService.setEvent(param);
}
}
详细信息组件:
export class DetailsComponent implements OnInit {
subscription;
constructor(private eventProxyService: EventProxyService) { }
ngOnInit() {
this.subscription = this.eventProxyService.getEvent().subscribe((param: any) => {
this.refresh(param);
);
}
refresh(param) {
this.record = param; //update record via new one passed from service
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}
该方法效果很好,但我对以下问题有点困惑:
1) 因为我也在 update()
方法中调用 this.eventProxyService.setEvent(param);
就像在 create()
方法中一样,调用这个是不是个好主意在我的服务级别而不是组件级别?
2) 上面 CreateComponent
中触发 next() 方法时有什么问题吗?
我个人认为你目前的做法是完全有效的。您可以考虑添加限制规则,就像您已经提到的那样,只从您的服务层调用 setEvent
。这将改善组件的单一职责。无论如何,我认为这真的取决于项目的规模和生命周期。对于开始一个项目,所以肯定会像你一样做!
我认为你的设计非常适合 BehaviourSubject
(至少在我看来是这样)。
我可以建议的一件事是在单例服务中尝试 BehaviourSubject
。 (在你的情况下我没注意到)
what if I call the setEvent() in the create() method of my service (not this proxyService, I meant other service where I keep my CRUD methods)
如果我正确理解了你的问题,答案是否定的。
因为从另一个服务调用一个服务方法是不可取的,所以保持简单。在实际项目中,一旦执行了 CRUD 方法,我们将尝试借助服务方法(但不在服务中)从组件本身刷新数据。
I call the next() via setEvent() method in the CRUD methods, after create, update, delete operations are completed. Is it a good approach, it is there a better way in real world examples e.g. calling next() in the service (CRUD) as I asked in the first question
是的,这是一个很好的方法。