import { HttpClient, HttpRequest, HttpResponse, HttpParams, HttpHeaders, } from '@angular/common/http'; import { NzMessageService, NzModalService, UploadFile } from 'ng-zorro-antd'; import { _HttpClient } from '@delon/theme'; import { Route, Router, ActivatedRoute, Params } from '@angular/router'; import { FormBuilder, FormGroup } from '@angular/forms'; import { Component, OnInit } from '@angular/core'; import {throttleTime} from "rxjs/operators"; import {debounceTime} from "rxjs-compat/operator/debounceTime"; @Component({ selector: 'zjcx', templateUrl: './zjcx.component.html', }) export class ZjcxComponent implements OnInit { pageIndex = 1; pageSize = 10; total = 0; data: any[] = []; loading = false; q = { pageIndex: 1, pageSize: 10, yhzh: '', zhmc: '', dfyhzh: '', dfzhmc: '', maxje: '' , minje: '', jysj: '', }; constructor( private http: _HttpClient, private httpClient: HttpClient, public msg: NzMessageService, private modalSrv: NzModalService, public router: Router, private fb: FormBuilder, ) { } ngOnInit() { this.msg.error("暂无数据"); } searchData(reset: boolean = false) { if (reset) { this.pageIndex = 1; } const yhzh = this.q.yhzh===null ? '':this.q.yhzh; const zhmc = this.q.zhmc===null ? '':this.q.zhmc; const dfyhzh = this.q.dfyhzh===null ? '':this.q.dfyhzh; const dfzhmc = this.q.dfzhmc===null ? '':this.q.dfzhmc; const minJe = this.q.minje===null ? '':this.q.minje; const maxJe = this.q.maxje===null ? '':this.q.maxje; const jysj = this.q.jysj===null ? '':this.q.jysj; const params = new HttpParams() .set('yhzh', yhzh) .set('zhmc', zhmc) .set('dfyhzh', dfyhzh) .set('dfzhmc', dfzhmc) .set('minJe', minJe) .set('maxJe', maxJe) .set('jysj', jysj) .set('pageIndex',''+this.pageIndex) .set('pageSize', ''+this.pageSize); this.loading=true; this.http .post('/download/zjfx/zjcx/getZjxx', params) .pipe(throttleTime(1000)) .subscribe((res: any) => { this.data = res.list; this.total = res.total; this.loading = false; },() => { this.loading = false; this.msg.error('任务查询失败,请重新再试!'); }); } }根据这个代码修改一下
时间: 2024-01-28 14:04:33 浏览: 186
根据你提供的代码,我看到你是在使用 `ng-zorro-antd` 的组件和 `_HttpClient` 来发送 HTTP 请求。为了解决查不到数据就会一直发送请求的问题,你可以在发送请求之前先判断一下当前是否已经有数据了,如果有,就不再发送请求。具体操作步骤如下:
1. 在组件类中添加一个变量来存储数据。
```typescript
data: any[] = [];
```
2. 在 `searchData()` 方法中,先判断一下当前是否已经有数据了,如果有,就直接返回不再发送请求。
```typescript
searchData(reset: boolean = false) {
if (reset) {
this.pageIndex = 1;
}
if (this.data.length > 0) {
return;
}
// 发送 HTTP 请求的代码
}
```
通过这种方法,你的应用程序将在有数据的情况下不再发送 HTTP 请求,从而解决了查不到数据就会一直发送请求的问题。
相关问题
import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse;爆红
这段代码的问题可能是缺少对Java 11中HttpClient类的导入。请确保在代码中包含以下导入语句:
```java
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
```
如果你已经包含了这些导入语句,那么可能是因为你的IDE没有正确配置Java 11 SDK。请检查你的IDE设置,并确保它已正确配置Java 11 SDK。
httpclient设置headers
HttpClient 可以使用多种方法来设置请求头(header)。以下是几种常见的方法:
1. 使用 setHeader 方法:
```java
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://example.com"))
.header("User-Agent", "Mozilla/5.0")
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
```
2. 使用 setHeaders 方法:
```java
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://example.com"))
.headers("User-Agent", "Mozilla/5.0")
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
```
3. 使用 HttpHeaders 类:
```java
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://example.com"))
.headers(headers -> {
headers.add("User-Agent", "Mozilla/5.0");
headers.add("Accept-Language", "en-US");
})
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
```
这些方法可以根据需要自由组合和扩展,以满足不同的请求头设置需求。
阅读全文