<script setup>
import { useOffsetPagination } from '@vueuse/core'
import { ref, watchEffect } from 'vue'
import { useFetch } from '@vueuse/core'
const posts = ref([])
const database = ref([])
watchEffect(async () => {
const response = await fetch('https://fakestoreapi.com/users')
posts.value = await response.json()
if (posts.value.length) {
posts.value.forEach((item) => {
database.value.push({ id: item.id, name: item.username })
})
}
})
function fetch(page, pageSize) {
return new Promise((resolve, reject) => {
const start = (page - 1) * pageSize
const end = start + pageSize
setTimeout(() => {
resolve(database.value.slice(start, end))
}, 100)
})
}
const data = ref([])
const page = ref(1)
const pageSize = ref(10)
fetchData({
currentPage: page.value,
currentPageSize: pageSize.value,
})
function fetchData({ currentPage, currentPageSize }) {
fetch(currentPage, currentPageSize).then((responseData) => {
data.value = responseData
})
}
const {
currentPage,
currentPageSize,
pageCount,
isFirstPage,
isLastPage,
prev,
next,
} = useOffsetPagination({
total: database.value.length,
page: 1,
pageSize,
onPageChange: fetchData,
onPageSizeChange: fetchData,
})
</script>
<template>
<div v-if="loading">
loading
</div>
<div>
posts: {{ posts }}
</div>
<div>
database: {{ database }}
</div>
<div>
data: {{ data }}
</div>
<p>Таблица:</p>
<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
</tr>
</thead>
<tr v-for="d in data" :key="d.id">
<td>{{ d.id }}</td>
<td>{{ d.username }}</td>
</tr>
</table>
<div class="my-4">
<button :disabled="isFirstPage" @click="prev">
prev
</button>
<button
v-for="item in pageCount"
:key="item"
:disabled="currentPage === item"
@click="currentPage = item"
>
{{ item }}
</button>
<button :disabled="isLastPage" @click="next">
next
</button>
</div>
</template>
<script setup>
import { useOffsetPagination } from '@vueuse/core'
import { ref, watchEffect } from 'vue'
import { useFetch } from '@vueuse/core'
const database = ref([])
const { data: posts } = useFetch('https://fakestoreapi.com/users').get().json()
posts.value.forEach((item) => {
database.value.push({ id: item.id, name: item.email })
})
function fetch(page, pageSize) {
return new Promise((resolve, reject) => {
const start = (page - 1) * pageSize
const end = start + pageSize
setTimeout(() => {
resolve(database.value.slice(start, end))
}, 100)
})
}
const data = ref([])
const page = ref(1)
const pageSize = ref(10)
fetchData({
currentPage: page.value,
currentPageSize: pageSize.value,
})
function fetchData({ currentPage, currentPageSize }) {
fetch(currentPage, currentPageSize).then((responseData) => {
data.value = responseData
})
}
const {
currentPage,
currentPageSize,
pageCount,
isFirstPage,
isLastPage,
prev,
next,
} = useOffsetPagination({
total: database.value.length,
page: 1,
pageSize,
onPageChange: fetchData,
onPageSizeChange: fetchData,
})
</script>
<template>
<div class="my-4">
<button :disabled="isFirstPage" @click="prev">
prev
</button>
<button
v-for="item in pageCount"
:key="item"
:disabled="currentPage === item"
@click="currentPage = item"
>
{{ item }}
</button>
<button :disabled="isLastPage" @click="next">
next
</button>
</div>
<div v-if="loading">
loading
</div>
<div>
{{ database }}
</div>
<div>
{{ data }}
</div>
<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
</tr>
</thead>
<tr v-for="d in data" :key="d.id">
<td>{{ d.id }}</td>
<td>{{ d.name }}</td>
</tr>
</table>
</template>
<script setup>
import { useOffsetPagination } from '@vueuse/core'
import { ref, watchEffect } from 'vue'
const posts = ref([])
const database = ref([])
const loading = ref(false)
const postForEach = () => {
posts.value.forEach((item) => {
database.value.push({ id: item.id, name: item.email })
})
}
watchEffect(async () => {
loading.value = true
try {
const response = await fetch('https://fakestoreapi.com/users')
posts.value = await response.json()
postForEach()
loading.value = false
} catch (err) {
console.log(err)
} finally {
loading.value = false
}
})
function fetch(page, pageSize) {
return new Promise((resolve, reject) => {
const start = (page - 1) * pageSize
const end = start + pageSize
setTimeout(() => {
resolve(database.value.slice(start, end))
}, 100)
})
}
const data = ref([])
const page = ref(1)
const pageSize = ref(10)
fetchData({
currentPage: page.value,
currentPageSize: pageSize.value,
})
function fetchData({ currentPage, currentPageSize }) {
fetch(currentPage, currentPageSize).then((responseData) => {
data.value = responseData
})
}
const {
currentPage,
currentPageSize,
pageCount,
isFirstPage,
isLastPage,
prev,
next,
} = useOffsetPagination({
total: database.value.length,
page: 1,
pageSize,
onPageChange: fetchData,
onPageSizeChange: fetchData,
})
</script>
<template>
<div class="my-4">
<button :disabled="isFirstPage" @click="prev">
prev
</button>
<button
v-for="item in pageCount"
:key="item"
:disabled="currentPage === item"
@click="currentPage = item"
>
{{ item }}
</button>
<button :disabled="isLastPage" @click="next">
next
</button>
</div>
<div v-if="loading">
loading
</div>
<div>
{{ database }}
</div>
<div>
{{ data }}
</div>
<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
</tr>
</thead>
<tr v-for="d in data" :key="d.id">
<td>{{ d.id }}</td>
<td>{{ d.name }}</td>
</tr>
</table>
</template>
import { useOffsetPagination } from '@vueuse/core'
import { useFetch } from '@vueuse/core'
import { ref, watchEffect } from 'vue'
const database = ref([])
const url = 'https://fakestoreapi.com/products'
const { data: posts } = useFetch(url).get().json()
for (let i = 0; i < 80; i++) {
database.value.push({ id: i, name: `user${i}`})
}
function fetch(page, pageSize) {
return new Promise((resolve, reject) => {
const start = (page - 1) * pageSize
const end = start + pageSize
setTimeout(() => {
resolve(database.value.slice(start, end))
}, 100)
})
}
const data = ref([])
const page = ref(1)
const pageSize = ref(10)
fetchData({
currentPage: page.value,
currentPageSize: pageSize.value,
})
function fetchData({ currentPage, currentPageSize }) {
fetch(currentPage, currentPageSize).then((responseData) => {
data.value = responseData
})
}
const {
currentPage,
currentPageSize,
pageCount,
isFirstPage,
isLastPage,
prev,
next,
} = useOffsetPagination({
total: database.value.length,
page: 1,
pageSize,
onPageChange: fetchData,
onPageSizeChange: fetchData,
})
Какую то ошибку ловит:
Сделал отдельный запрос на этой же странице:
Все работает: