keepAlive 列表页跳到详情页,再次返回时el-table滚动位置恢复。el-table 的固定列 会错位。

keepAlive 配置的列表页遇到了几个问题。

1、当列表页经路由跳转跳到详情页,从详情页返回到列表页时,el-table 的滚动位置无法被缓存,会回到顶部。

2、el-table 的固定列 会错位。

问题 1 解决:

  1 、配置需要缓存的页面

const routes = [
  { path: '/A', component: A },
  { path: '/B', component: B },
  { path: '/C', component: C },
  { path: '/D', component: D },
  {
    path: '/E',
    component: E,
    meta: { keepAlive: true }, // 默认不缓存
  },
  { path: '/F', component: F },
  { path: '/G', component: G },
];

2、举个简单里例子  在你的el-table 里面增加 ref  和 handleScroll函数

<template>
  <div>
    <!-- 使用 keep-alive 缓存组件 -->
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
    
    <!-- 假设这是你的 el-table -->
    <el-table ref="table" @scroll="handleScroll">
      <!-- 表格内容 -->
    </el-table>
  </div>
</template>

3、

 methods: {
    handleScroll(event) {
      this.scrollTop = event.target.scrollTop;
    },
    restoreScroll() {
      if (this.$refs.table) {
        this.$refs.table.bodyWrapper.scrollTop = this.scrollTop;
      }
    }
  },
  activated() {
    // 当组件被重新激活时,恢复滚动位置
    this.$nextTick(() => {
      this.restoreScroll();
    });
  },
  beforeRouteLeave(to, from, next) {
    // 在离开路由前保存滚动位置
    if (this.$refs.table) {
      this.scrollTop = this.$refs.table.bodyWrapper.scrollTop;
    }
    next();
  }

==========↑↑↑↑↑======= 直接复制代码就能解决滚动问题。=======↑↑↑↑↑==========

第二个问题解决方案

1、因为咱们的页面时缓存的 所以咱们要在activited() 里面触发。

acitivited(){
    if (this.$refs.myTable && this.$refs.myTable.doLayout) {
          this.$refs.table.doLayout();  
        }
        window.onresize = () => {
          if (this.$refs.myTable&& this.$refs.myTable.doLayout) {
        this.$refs.table.doLayout(); //页面返回时触发
          }
    };
}

========↑↑↑↑↑↑↑========这样两个问题就解决了=========↑↑↑↑↑↑↑=======

===============↓↓↓↓↓=============优化:=========↓↓↓↓↓==============

因为页面会多处使用,咱们就需要做一些优化:

把他提出来,写一个mixnis

第一步:创建一个 Mixin 文件

// src/mixins/scrollRestore.js

export default {
  data() {
    return {
      scrollTop: 0,
    };
  },
  methods: {
    handleScroll(event) {
      this.scrollTop = event.target.scrollTop;
    },
    restoreScroll(refName = 'table') {
      const table = this.$refs[refName];
      if (table && table.bodyWrapper) {
        table.bodyWrapper.scrollTop = this.scrollTop;
      }
    },
  },
  activated() {
    this.$nextTick(() => {
      this.restoreScroll();
    });
  },
  beforeRouteLeave(to, from, next) {
    const table = this.$refs.table;
    if (table && table.bodyWrapper) {
      this.scrollTop = table.bodyWrapper.scrollTop;
    }
    next();
  },
};

第二步:在组件中引入并使用这个 Mixin

<script>
import scrollRestore from '@/mixins/scrollRestore';

export default {
  mixins: [scrollRestore],
};
</script>

========↑↑↑↑↑↑↑========这里的优化只针对了table=========↑↑↑↑↑↑↑=======

如果你想兼容其他滚动容器(如 div、el-scrollbar 等),可以检测 $el 或者指定 container 元素:

restoreScroll(containerKey = 'scrollContainer') {
  const container = this.$refs[containerKey] || this.$el;
  if (container) {
    container.scrollTop = this.scrollTop;
  }
}
在使用Element UI的el-table组件查看数据详情并返回,如果不希望表刷新,可以通过以下几种方法实现: 1. **使用Vue的keep-alive组件**: 通过将表组件包裹在keep-alive组件中,可以缓存组件的状态,避免重新加载数据。 ```vue <template> <div> <keep-alive> <component :is="currentComponent"></component> </keep-alive> <el-button @click="switchComponent('List')">返回表</el-button> <el-button @click="switchComponent('Detail')">查看详情</el-button> </div> </template> <script> export default { data() { return { currentComponent: 'List' }; }, methods: { switchComponent(component) { this.currentComponent = component; } }, components: { List: { template: `<el-table :data="tableData"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table>`, data() { return { tableData: [ { date: '2016-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' } ] }; } }, Detail: { template: `<div>这里是详情页面</div>` } } }; </script> ``` 2. **使用Vuex管理状态**: 通过Vuex管理表格数据的状态,确保在返回数据不会被重新加载。 ```vue <template> <div> <el-table :data="tableData"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> <el-button @click="goToDetail">查看详情</el-button> </div> </template> <script> import { mapState, mapActions } from 'vuex'; export default { computed: { ...mapState(['tableData']) }, methods: { ...mapActions(['fetchTableData']), goToDetail() { this.$router.push('/detail'); } }, created() { this.fetchTableData(); } }; </script> ``` 3. **使用路由缓存**: 通过路由配置中的meta属性和`<keep-alive>`标签来缓存路由组件。 ```vue <template> <div> <router-view v-slot="{ Component }"> <keep-alive> <component :is="Component" /> </keep-alive> </router-view> <el-button @click="goBack">返回表</el-button> </div> </template> <script> export default { methods: { goBack() { this.$router.push('/list'); } } }; </script> ``` 在路由配置中: ```javascript const routes = [ { path: '/list', component: List, meta: { keepAlive: true } }, { path: '/detail', component: Detail } ]; ``` 通过以上方法,可以实现el-table查看数据详情并返回不刷新的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值