我没有得到 rxjs 6 with angular 6 with interval, switchMap, and map
I dont get rxjs 6 with angular 6 with interval, switchMap, and map
我想将我的 rxjs 代码更新为 6,我不明白。
在我得到以下每 5 秒轮询一次新数据之前:
import { Observable, interval } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
var result = interval(5000).switchMap(() => this._authHttp.get(url)).map(res => res.json().results);
现在……当然,它坏了,文档让我无处可去。
如何编写以上内容以符合 rxjs 6?
谢谢
代码应该如下所示。您需要使用 pipe
运算符。
import { interval } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
const result = interval(5000).pipe(
switchMap(() => this._authHttp.get(url)),
map(res => res.results)
)
经过大量研究,我可以从 RxJs 6 Angular 6 中得出以下更新方法。
搜索 API 在每间隔 5 秒后调用一次,一旦计数 > 5 就会取消订阅:
let inter=interval(5000)
let model : ModelComponent;
model=new ModelComponent();
model.emailAddress="mdshahabaz.khan@gmail.com";
let count=1;
this.subscriber=inter.pipe(
startWith(0),
switchMap(()=>this.asyncService.makeRequest('search',model))
).subscribe(response => {
console.log("polling")
console.log(response.list)
count+=1;
if(count > 5){
this.subscriber.unsubscribe();
}
});
API 请求:
makeRequest(method, body) : Observable<any> {
const url = this.baseurl + "/" + method;
const headers = new Headers();
this.token="Bearer"+" "+localStorage.getItem('token');
headers.append('Authorization', this.token);
headers.append('Content-Type','application/json');
const options = new RequestOptions({headers: headers});
return this.http.post(url, body, options).pipe(
map((response : Response) => {
var json = response.json();
return json;
})
);
}
不要忘记取消订阅以避免内存泄漏。
ngOnDestroy(): void {
if(this.subscriber){
this.subscriber.unsubscribe();
}
}
我想将我的 rxjs 代码更新为 6,我不明白。
在我得到以下每 5 秒轮询一次新数据之前:
import { Observable, interval } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
var result = interval(5000).switchMap(() => this._authHttp.get(url)).map(res => res.json().results);
现在……当然,它坏了,文档让我无处可去。
如何编写以上内容以符合 rxjs 6?
谢谢
代码应该如下所示。您需要使用 pipe
运算符。
import { interval } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
const result = interval(5000).pipe(
switchMap(() => this._authHttp.get(url)),
map(res => res.results)
)
经过大量研究,我可以从 RxJs 6 Angular 6 中得出以下更新方法。
搜索 API 在每间隔 5 秒后调用一次,一旦计数 > 5 就会取消订阅:
let inter=interval(5000)
let model : ModelComponent;
model=new ModelComponent();
model.emailAddress="mdshahabaz.khan@gmail.com";
let count=1;
this.subscriber=inter.pipe(
startWith(0),
switchMap(()=>this.asyncService.makeRequest('search',model))
).subscribe(response => {
console.log("polling")
console.log(response.list)
count+=1;
if(count > 5){
this.subscriber.unsubscribe();
}
});
API 请求:
makeRequest(method, body) : Observable<any> {
const url = this.baseurl + "/" + method;
const headers = new Headers();
this.token="Bearer"+" "+localStorage.getItem('token');
headers.append('Authorization', this.token);
headers.append('Content-Type','application/json');
const options = new RequestOptions({headers: headers});
return this.http.post(url, body, options).pipe(
map((response : Response) => {
var json = response.json();
return json;
})
);
}
不要忘记取消订阅以避免内存泄漏。
ngOnDestroy(): void {
if(this.subscriber){
this.subscriber.unsubscribe();
}
}