切换路由默认回到顶部功能

方法一:我们可以在每次组件重新渲染的时候触发回到顶部


// updated 不会保证所有的子组件也都被重新渲染完毕。如果你希望等待整个视图都渲染完毕,可以在 updated 内部使用 vm.$nextTick: 
updated() {  
    // 进入页面强制滚动到顶部
    let dom = document.getElementsByClassName('el-main')[0];//需要哪个组件回到顶部,写对应的类名
    dom.scrollTo(0, 0);
  }

方法二:路由文件中做修改处理,路由切换时候赋值scrollTop


const router = createRouter({
       history: createWebHistory(process.env.BASE_URL),
       routes: [],
       scrollBehavior(to, from, savedPosition) {
              (to.path !== from.path) && (document.getElementById('main').scrollTop = 0)
       } 
})
笨鸟飞呀飞~
原文地址:https://www.cnblogs.com/geekfeier/p/15696878.html