具体实现代码如下:
<template>
<div>
<ul>
<li v-for="item in currentPageData" :key="item.id">{{ item.name }}</li>
</ul>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1, // 当前页码
pageSize: 10, // 每页显示的数量
totalData: [], // 总数据
currentPageData: [] // 当前页数据
};
},
computed: {
totalPages() {
return Math.ceil(this.totalData.length / this.pageSize); // 总页数
}
},
methods: {
fetchData() {
// 向后端发送请求获取数据,并将数据存储在totalData中
axios.get('/api/data', {
params: {
page: this.currentPage,
size: this.pageSize
}
}).then(response => {
this.totalData = response.data;
this.currentPageData = response.data.slice((this.currentPage - 1) *
this.pageSize, this.currentPage * this.pageSize);
});
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++; // 更新当前页码
this.fetchData(); // 重新获取数据
}
}
},
mounted() {
this.fetchData(); // 在组件挂载时获取数据
}
};
</script>
以上是编程学习网小编为您介绍的“vuejs下一页功能实现代码介绍”的全面内容,想了解更多关于
vuejs 内容,请继续关注编程基础学习网。