在Angular 7中,可以使用RxJs库来轮询第一个端点的响应端点。下面是一个示例解决方法的代码:
npm install rxjs
import { interval } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
pollEndpoint() {
return this.http.get('YOUR_ENDPOINT_URL');
}
pollResponseEndpoint() {
interval(1000) // 每秒执行一次
.pipe(
switchMap(() => this.pollEndpoint())
)
.subscribe(response => {
// 处理响应
console.log(response);
});
}
pollResponseEndpoint()
方法来启动轮询:ngOnInit() {
this.pollResponseEndpoint();
}
这样,你就可以在Angular 7中使用RxJs来轮询第一个端点的响应端点了。请确保将YOUR_ENDPOINT_URL
替换为实际的端点URL。