更新订阅中的值
update value in subscription
我有一个服务周期性 api 调用会更改 url 如果这是第一次调用它。
它有一个用于生成 api 调用的间隔订阅,我不能使用全局变量,因为它是一个可以在不移除注入器的情况下调用的服务。
getData(init: boolean): Observable<any> {
const refreshInterval = 5000;
const interval = observableInterval(refreshInterval).pipe(startWith(0))
return interval.pipe(
flatMap(() => this.http.get<any>(this.getUrl(init))),
map((data) => this.doWhateverWithData(data)),
);
}
getUrl(init: boolean): string {
return init ? url1 : url2;
}
我的问题是 init 没有改变,所以 url 也没有改变。
我该如何解决?
正如我所说,由于我的代码,我无法使用 this.init。
你的用例有点特殊,但如果你真的想在流订阅期间更改变量,你可以使用 BehaviorSubject
和 withLatestFrom
init$=new BehaviorSubject('initialvalue');
getData(init: boolean): Observable<any> {
const refreshInterval = 5000;
const interval = observableInterval(refreshInterval).pipe(startWith(0))
return interval.pipe(
withLatestFrom(init$),
flatMap(res=>this.apiSql.get<any>(this.getUrl(res[1]))),
map((data) => this.doWhateverWithData(data)),
);
}
您始终可以通过 init$.next('new value')
更改 init$ 缓存值
在 rxjs@6 中,您可以使用 from
作为独立函数:
import { from } from 'rxjs';
另请参阅迁移到 rxjs6 指南
https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths
更新
您需要切换到管道语法,确保导入所有从 rxjs/operators
中使用的运算符。例如:
import { map, filter, catchError, mergeMap } from 'rxjs/operators';
getDataListApi() {
return this.http.get("/api/method")
pipe(
map((data: any[]) => {
this.products = data; // here you can update your data as your requirement
return true;
}), catchError( error => {
return throwError( 'Something went wrong!' )
});
)
}
我解决了。
只需更改间隔中的开始并检查 flapMap
中的值
getData(): Observable<any> {
const refreshInterval = 5000;
const interval = observableInterval(refreshInterval).pipe(startWith(-1))
return interval.pipe(
flatMap((index) => this.http.get<any>(this.getUrl(index))),
map((data) => this.doWhateverWithData(data)),
);
}
getUrl(index: number): string {
return (index === -1) ? url1 : url2;
}
我有一个服务周期性 api 调用会更改 url 如果这是第一次调用它。
它有一个用于生成 api 调用的间隔订阅,我不能使用全局变量,因为它是一个可以在不移除注入器的情况下调用的服务。
getData(init: boolean): Observable<any> {
const refreshInterval = 5000;
const interval = observableInterval(refreshInterval).pipe(startWith(0))
return interval.pipe(
flatMap(() => this.http.get<any>(this.getUrl(init))),
map((data) => this.doWhateverWithData(data)),
);
}
getUrl(init: boolean): string {
return init ? url1 : url2;
}
我的问题是 init 没有改变,所以 url 也没有改变。
我该如何解决?
正如我所说,由于我的代码,我无法使用 this.init。
你的用例有点特殊,但如果你真的想在流订阅期间更改变量,你可以使用 BehaviorSubject
和 withLatestFrom
init$=new BehaviorSubject('initialvalue');
getData(init: boolean): Observable<any> {
const refreshInterval = 5000;
const interval = observableInterval(refreshInterval).pipe(startWith(0))
return interval.pipe(
withLatestFrom(init$),
flatMap(res=>this.apiSql.get<any>(this.getUrl(res[1]))),
map((data) => this.doWhateverWithData(data)),
);
}
您始终可以通过 init$.next('new value')
在 rxjs@6 中,您可以使用 from
作为独立函数:
import { from } from 'rxjs';
另请参阅迁移到 rxjs6 指南
https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths
更新
您需要切换到管道语法,确保导入所有从 rxjs/operators
中使用的运算符。例如:
import { map, filter, catchError, mergeMap } from 'rxjs/operators';
getDataListApi() {
return this.http.get("/api/method")
pipe(
map((data: any[]) => {
this.products = data; // here you can update your data as your requirement
return true;
}), catchError( error => {
return throwError( 'Something went wrong!' )
});
)
}
我解决了。 只需更改间隔中的开始并检查 flapMap
中的值getData(): Observable<any> {
const refreshInterval = 5000;
const interval = observableInterval(refreshInterval).pipe(startWith(-1))
return interval.pipe(
flatMap((index) => this.http.get<any>(this.getUrl(index))),
map((data) => this.doWhateverWithData(data)),
);
}
getUrl(index: number): string {
return (index === -1) ? url1 : url2;
}