Angular RXJS主体与行为主体
我有多个请求,动态参数数组参数在可观察的时间间隔内。那么,如何基于数组参数返回主题呢。因为Angular RXJS主体与行为主体,angular,rxjs5,rxjs6,Angular,Rxjs5,Rxjs6,我有多个请求,动态参数数组参数在可观察的时间间隔内。那么,如何基于数组参数返回主题呢。因为行为主体包含其中的所有数据 初始化主题 getSchedularData: BehaviorSubject < any > = new BehaviorSubject < any > (null); constructor(private httpClient: HttpClient, ) { SchedulerStore.select('jobSchedulerState')
行为主体
包含其中的所有数据
初始化主题
getSchedularData: BehaviorSubject < any > = new BehaviorSubject < any > (null);
constructor(private httpClient: HttpClient, ) {
SchedulerStore.select('jobSchedulerState')
.subscribe(response => {
this.schedularDataCollector = response;
});
}
this.schedulerService.startScheduler(this.channelList1)
.subscribe((value) => {
// console.log(value);
// tslint:disable-next-line:forin
for (const keys in value) {
this.schedularData[keys] = value[keys];
}
});
服务
Observable.interval((!this.backChannelEnvironment.schedularInterval) ? 10000 : this.backChannelEnvironment.schedularInterval)
.pipe(
map(() => {
/**
* dispatch request for schedular for requesting http request for channel
*/
this.SchedulerStore.dispatch(new SchedulerAction.GetScheduler(Channels));
})
)
.subscribe(() => {
/**
* get data from schedular store and return it at schedular interval
*/
if (this.schedularDataCollector != null) {
if (JSON.stringify(this.schedularDataCollector['jobScheduler']) !==
JSON.stringify(this.getSchedularData.value)) {
this.getSchedularData.next(this.schedularDataCollector['jobScheduler']);
}
}
});
return this.getSchedularData.asObservable();
请填写以下代码:- 这里我提到了一些其他方法,如debounceTime()和distinctUntilChanged()可以根据您的需求进行更改
@Output() completeMethod: EventEmitter<any> = new EventEmitter();
customEvent: Event;
private yourSubject = new Subject<string>();
constructor(){
this.yourSubject.asObservable().pipe(filter(data => data != null),
debounceTime(1000), distinctUntilChanged()).subscribe(value => {
this.loading = true;
this.completeMethod.emit({
originalEvent: this,
query: value
});
});
}
ngAfterViewInit() {
this.inputEL.nativeElement.addEventListener('keyup', (event) => {
this.customEvent = event;
this.yourSubject.next(event.target.value);
});
}
@Output()completeMethod:EventEmitter=neweventEmitter();
自定义事件:事件;
private yourSubject=新主题();
构造函数(){
this.yourSubject.asObservable().pipe(过滤器(数据=>data!=null),
debounceTime(1000),distinctUntilChanged())。订阅(值=>{
这是。加载=真;
此.completeMethod.emit({
原始事件:这个,
查询:值
});
});
}
ngAfterViewInit(){
this.inputEL.nativeElement.addEventListener('keyup',(事件)=>{
this.customEvent=事件;
this.yourSubject.next(event.target.value);
});
}
您可以尝试以下方法,因为我们的实现与您的几乎相同:
private requests: Request[];
private observableRequests: BehaviorSubject<Request[]>;
constructor() {
this.requests = new Array<Request>;
this.observableRequests = <BehaviorSubject<Request[]>>new BehaviorSubject([]);
}
get requests() {
return this.observableRequests.asObservable();
}
addRequest(request: Request) {
this.requests.push(request);
this.observableRequests.next(this.requests);
}
私人请求:请求[];
私人观察任务:行为主体;
构造函数(){
this.requests=新数组;
this.observeRequests=新行为主体([]);
}
获取请求(){
返回此.observeRequests.asObservable();
}
addRequest(请求:请求){
this.requests.push(请求);
this.observeRequests.next(this.requests);
}
这里,只要调用数组,所有请求对象都将返回addRequest。
您必须根据您的要求在该方法中进行一些变通。如果您可以在stackblitz中复制您的问题。。?那太棒了!在排序行为中,主题包含一个数组对象,对于每个请求,其中都有一个参数。根据参数,我必须过滤行为主题并返回结果。它工作正常,但当我使用不同的参数请求服务两次时,它会用2个请求覆盖1个请求的结果,并在两个请求中返回结果2。我的参数是动态的,因此无法为每个参数创建自定义行为。或者它没有发生。请复制。