1.首先给需要的页面添加缓存keep-alive
<div id="app">
<keep-alive>
<router-view></router-view>
</keep-alive>
</div>
2记录滚动条位置,并在页面显示时设置它
beforeRouteLeave(to, from, next) : 离开路由之前执行的函数,可用于页面的反向传值,页面跳转。
<template>
<el-table class="table" :data="tableData" border ref='table' >
</el-table>
</template>
<script>
export default{
data(){
return{
scrollTop: 0,//记录滚动条位置
}
},
//离开路由之前执行的函数
beforeRouteLeave(to, from, next) {
console.log("beforeRouteLeave")
// 获取table滚动条的位置
// 记录滚动条位置
this.$refs.talbe.bodyWrapper.addEventListener(
'scroll',
(res) => {
this.scrollTop = res.target.scrollTop
next()
},
false
)
next()
},
// 组件激活时触发
activated() {
// 还原滚动条位置
this.$nextTick(() => {
setTimeout(() => {
// 设置滚动条的位置
this.$refs.monthlyPlanTable.bodyWrapper.scrollTop = this.scrollTop
}, 100) // 需要设置延迟,否则无效
})
},
}
</script>