keepAlive保存滚动位置

vue2

 data() {
    return {
      scTop: 0//滚动距离顶部的位置
    }
  },
  // 进入该路由时
  activated() {
    document.documentElement.scrollTop = this.scTop
  },

  // 离开该路由前
  beforeRouteLeave(to, from, next) {
    this.scTop = document.documentElement.scrollTop || 0
    next()
  },

vue3 组合式保存位置

<script setup>
import {onActivated, ref } from "vue";
import {onBeforeRouteLeave} from 'vue-router';

const scrollTop = ref(0)
// 离开该路由前
onBeforeRouteLeave(() => {
 scrollTop.value = document.documentElement.scrollTop || 0
})

 // 进入该路由时
onActivated(() => {
  document.documentElement.scrollTop =  scrollTop.value
})
</script>