Angular HTTP API响应缓存(仅请求一次)
我正在进行一个项目,在该项目中,我可以在其中消耗API来获取订单,这是每次我导航到另一个部分并返回消耗API的位置时,都会重复该请求。我希望它一旦进入该部分就可以咨询API,如果我回到另一个部分并回来,它将不会再次致电API。我尝试了SharePlay,该方法是ObtenerorDenxoperario”,我给您留下了控制台,服务和组件的屏幕截图:
console.log(this.foundRol. RolName);
this.authService
.ObtenerOrdenXOperario(usuario, where)
.subscribe ((response) => {
if (response) { ´
response.forEach((element) => {
if (element. Proceso === 'ACTIVA') {
if (this.interval) {
clearInterval(this.interval);
}
const navigationExtras: NavigationExtras = {
state: {
orden: element,
estadoorden: element.Estado,
manual: false,
},
};
ObtenerOrdenxOperario(idusuario, where): Observable <any> {
let params = new HttpParams().set("IdUsuario", idUsuario).set("Where", where)
let headers = new HttpHeaders().set('Content-Type', 'application/json');
return this.httpclient.get(environment.urlColector + "/
obtenerOrdenxOperario", { headers: headers, params: params }).pipe(
shareReplay(1),
)
};
I am doing a project in which I consume an api to obtain orders, the thing is that every time I navigate to another section and go back to where the api is being consumed, it repeats the request. I want it to consult the api ONCE it enters that section, and if I go to another and come back, it won't call the api again. I tried with shareplay, the method is ObtenerOrdenXOperario", I leave you a screenshot of the console, the service and the component:
console.log(this.foundRol. RolName);
this.authService
.ObtenerOrdenXOperario(usuario, where)
.subscribe ((response) => {
if (response) { ´
response.forEach((element) => {
if (element. Proceso === 'ACTIVA') {
if (this.interval) {
clearInterval(this.interval);
}
const navigationExtras: NavigationExtras = {
state: {
orden: element,
estadoorden: element.Estado,
manual: false,
},
};
ObtenerOrdenxOperario(idusuario, where): Observable <any> {
let params = new HttpParams().set("IdUsuario", idUsuario).set("Where", where)
let headers = new HttpHeaders().set('Content-Type', 'application/json');
return this.httpclient.get(environment.urlColector + "/
obtenerOrdenxOperario", { headers: headers, params: params }).pipe(
shareReplay(1),
)
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论