我想动态获取总页数,我的API在“x-total-count = 50”中的响应标头中返回总页数,但无法真正弄清楚如何将该变量获取到我的前端,这是我的代码
const sort = ref({ column: 'licensePlate', direction: 'asc' as const })
const page = ref(1)
const pageCount = ref(10)
const pageTotal = ref(50) //This value should be dynamic coming from the API
const pageFrom = computed(() => (page.value - 1) * pageCount.value + 1)
const pageTo = computed(() => Math.min(page.value * pageCount.value, pageTotal.value))
// Data
const { data: cars, status} = await useLazyAsyncData<{
licensePlate: string
arrivalDate: string
charge: number
}[]>('cars', (response) => ($fetch as any)(`http://localhost:3001/deported`, {
query: {
q: search.value,
'_page': page.value,
'_limit': pageCount.value,
'_sort': sort.value.column,
'_order': sort.value.direction
}
}), {
default: () => [],
watch: [page, search, pageCount, sort]
})
到目前为止还无法尝试任何事情
我相信您可以使用原始获取和访问响应标头,如下所示:
const { data } = useAsyncData(async () => {
const response = await $fetch.raw('http://localhost:3001/deported', {
query: {
q: search.value,
_page: page.value,
_limit: pageCount.value,
_sort: sort.value.column,
_order: sort.value.direction,
},
});
const pages = response.headers.get('x-total-count');
pageTotal.value = pages ? +pages : 0;
/** Assuming you are parsing response as json */
return await response.json();
});
取自ofetch docs(获取nuxt使用的库)