Angular HTTP API响应缓存(仅请求一次)

发布于 2025-02-10 04:20:09 字数 1470 浏览 2 评论 0原文

我正在进行一个项目,在该项目中,我可以在其中消耗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:

enter image description here

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,
                },
              };

Service:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文