vue3.0+element plus 实现使用分页功能页面刷新后维持页面不变

6 篇文章 0 订阅
2 篇文章 0 订阅

说明:案例只有分页栏,没有数据。实现的效果体现在分页栏页码的变化上。

构建分页

在element plus挑选一个分页组件,创建test页面


<template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400">
    </el-pagination>
</template>

<script>
  export default {
    methods: {
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      }
    },
    data() {
      return {
      };
    }
  }
</script>

页面展示

在这里插入图片描述

点击页码可以实现页面切换。如果点击浏览器的刷新页面按钮,页码并不会维持在当前页面,而是会切换到首页。我们要实现的功能就是浏览器刷新后让当前页面刷新并且不会切换到首页。

修改代码

<template>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10]"
    :page-size="pagesize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  >
  </el-pagination>
</template>

<script>

export default {
  data() {
    return {
      total: 120, // 总共多少数据
      currentPage: 1, // 当前页
      pagesize: 10, // 当前页显示多少条
    };
  },
  created() {
    this.Info()
  },
  methods: {
    Info(val) {
      // 用来判断页面是否被刷新
      if (!val) {
        this.currentPage = this.$route.query.page;
        val = this.$route.query.page ? this.$route.query.page : 1;
      }
      const page = parseInt(val) - 1;
      console.log(page)
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.$router.replace({
        path: this.$route.path,
        query: {
          page: this.currentPage,
        },
      });
      this.Info(val);
    },
  },
};
</script>
最终效果

在这里插入图片描述

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
前端代码(使用Vue3和Element Plus): ``` <template> <div> <el-table :data="tableData" stripe> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" layout="total, prev, pager, next" :total="total"> </el-pagination> </div> </template> <script> import { ref } from 'vue'; import { getTableData } from '@/api/example'; export default { setup() { const currentPage = ref(1); const pageSize = ref(10); const total = ref(0); const tableData = ref([]); async function getData() { const params = { currentPage: currentPage.value, pageSize: pageSize.value, }; const res = await getTableData(params); if (res.code === 200) { tableData.value = res.data.list; total.value = res.data.total; } } function handleCurrentChange(page) { currentPage.value = page; getData(); } getData(); return { currentPage, pageSize, total, tableData, handleCurrentChange, }; }, }; </script> ``` 后端代码(使用Spring Boot 2): ``` @GetMapping("/tableData") public CommonResult<PageResult<TableData>> getTableData(@RequestParam(required = false, defaultValue = "1") Integer currentPage, @RequestParam(required = false, defaultValue = "10") Integer pageSize) { PageResult<TableData> pageResult = tableDataService.getTableData(currentPage, pageSize); return CommonResult.success(pageResult); } ``` 其,`TableData`为实体类,`PageResult`为分页结果类。`tableDataService`为对应的Service类,用于查询数据
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值