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 10:04:33 浏览: 194
根据你提供的代码,我看到你是在使用 `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 请求,从而解决了查不到数据就会一直发送请求的问题。
阅读全文