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 21:04:33 浏览: 197
根据你提供的代码,我看到你是在使用 `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。
angular中用httpClient发送http请求,携带请求头
在Angular中使用HttpClient发送HTTP请求并携带请求头的步骤如下:
1. **导入HttpClientModule**:首先需要在你的Angular模块(通常是`app.module.ts`)中导入`HttpClientModule`。
```typescript
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
```
2. **注入HttpClient**:在你的服务或组件中注入`HttpClient`。
```typescript
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ApiService {
private apiUrl = 'https://api.example.com';
constructor(private http: HttpClient) { }
getData(): Observable<any> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer your-token'
});
return this.http.get<any>(`${this.apiUrl}/data`, { headers });
}
}
```
3. **发送请求**:在你的组件中使用该服务发送请求。
```typescript
import { Component, OnInit } from '@angular/core';
import { ApiService } from './api.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private apiService: ApiService) { }
ngOnInit() {
this.apiService.getData().subscribe(
data => {
console.log(data);
},
error => {
console.error(error);
}
);
}
}
```
通过以上步骤,你可以在Angular中使用HttpClient发送带有请求头的HTTP请求。
阅读全文
相关推荐
















