vue 设置跳转页面回到顶部

第一种: 在 main.js 设置

router.afterEach((to,from,next) => {
    window.scrollTo(0,0);
});

第二种:动态路由设置

scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
        return savedPosition
    }
    return {x: 0, y: 0}
}

第三种:在APP.vue 设置

// 使用watch 监听$router的变化,
 watch: {
    '$route': function(to,from){
     document.body.scrollTop = 0
      document.documentElement.scrollTop = 0
    }
  }

 

posted @ 2024-05-23 17:12  薛定谔_猫  阅读(917)  评论(0)    收藏  举报