const list = pageindex > totalPage ? [] : article.slice(start, end)
时间: 2024-05-30 10:11:13 浏览: 18
这行代码的意思是:如果当前页码超出了总页数,那么返回一个空数组 `[]`,否则返回 `article` 数组中从 `start` 到 `end` 索引的元素组成的子数组 `list`。其中 `start` 和 `end` 的计算公式如下:
- `start = (pageindex - 1) * pagesize`
- `end = pageindex * pagesize`
其中,`pageindex` 表示当前页码,`pagesize` 表示每页显示的文章数量,`totalPage` 表示总页数,`article` 表示所有文章组成的数组。
相关问题
优化这段代码:import { reactive, toRaw } from 'vue' // 分页钩子函数 interface Options { page?: number size?: number fetchFun: (_arg: any) => Promise<any> params?: Record<any, any> firstLoading?: boolean } export function usePaging(options: Options) { const { page = 1, size = 15, fetchFun, params = {}, firstLoading = false } = options // 记录分页初始参数 const paramsInit: Record<any, any> = Object.assign({}, toRaw(params)) // 分页数据 const pager = reactive({ page, size, loading: firstLoading, count: 0, lists: [] as any[], extend: {} as Record<string, any> }) // 请求分页接口 const getLists = () => { pager.loading = true return fetchFun({ page_no: pager.page, page_size: pager.size, ...params }) .then((res: any) => { pager.count = res?.count pager.lists = res?.lists pager.extend = res?.extend return Promise.resolve(res) }) .catch((err: any) => { return Promise.reject(err) }) .finally(() => { pager.loading = false }) } // 重置为第一页 const resetPage = () => { pager.page = 1 getLists() } // 重置参数 const resetParams = () => { Object.keys(paramsInit).forEach((item) => { params[item] = paramsInit[item] }) getLists() } return { pager, getLists, resetParams, resetPage } }
以下为优化后的代码:
import { reactive, toRaw } from 'vue'
interface Options {
page?: number
size?: number
fetchFun: (_arg: any) => Promise<any>
params?: Record<any, any>
firstLoading?: boolean
}
export function usePaging(options: Options) {
const { page = 1, size = 15, fetchFun, params = {}, firstLoading = false } = options
const paramsInit: Record<any, any> = toRaw(params)
const pager = reactive({
page,
size,
loading: firstLoading,
count: 0,
lists: [] as any[],
extend: {} as Record<string, any>
})
const getLists = async () => {
pager.loading = true
try {
const res = await fetchFun({
page_no: pager.page,
page_size: pager.size,
...params
})
pager.count = res?.count
pager.lists = res?.lists
pager.extend = res?.extend
return Promise.resolve(res)
} catch (err) {
return Promise.reject(err)
} finally {
pager.loading = false
}
}
const resetPage = () => {
pager.page = 1
getLists()
}
const resetParams = () => {
Object.assign(params, paramsInit)
getLists()
}
return {
pager,
getLists,
resetParams,
resetPage
}
}
优化的部分包括:
1. 使用 async/await 替代 Promise.then() 语法,使代码更加简洁易读,同时也更具可读性。
2. 重置参数部分使用 Object.assign() 替代 Object.keys(paramsInit).forEach() 语法,可以更简洁地实现参数重置操作。
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('任务查询失败,请重新再试!'); }); } }根据这个代码修改一下
根据你提供的代码,我看到你是在使用 `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 请求,从而解决了查不到数据就会一直发送请求的问题。