简体   繁体   English

Angular:如何将“优先级”设置为 HttpClient

[英]Angular: how set "priority" to HttpClient

Priority hints indicate the relative priority of resources to the browser.优先级提示表明资源对浏览器的相对优先级。 They can enable optimal loading and improve Core Web Vitals.它们可以实现最佳负荷并改善 Core Web Vitals。

From https://web.dev/priority-hints/来自https://web.dev/priority-hints/

example with fetch API:获取 API 的示例:

<script>
  fetch('https://example.com/', {priority: 'low'})
  .then(data => {
    // Trigger a low priority fetch
  });
</script>

how set "priority" to Angular HttpClient ?如何将“优先级”设置为 Angular HttpClient

I have tried to force a priority setting:我试图强制设置优先级:

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <button (click)="makeRequest()">make request</button><br />
  <pre>{{ data | json }}</pre>`,
})
export class AppComponent implements OnInit {
  data: any;

  constructor(private httpClient: HttpClient) {}

  ngOnInit(): void {
    this.makeRequest();
  }

  makeRequest() {
    this.data = null;
    this.httpClient
      .get('https://jsonplaceholder.typicode.com/todos/1', {
        priority: 'low',
      } as any)
      .subscribe((result) => (this.data = result));
  }
}

online 在线的

but doesn't works... priority is always hight但不起作用......优先级总是很高

also suggested to angular team https://github.com/angular/angular/issues/45426还建议给 angular 团队https://github.com/angular/angular/issues/45426

You can use like below:您可以像下面这样使用:

this.http.get<YourConfig>(this.yourConfigUrl, {priority: 'low'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM