vue跳转新页面如何回到顶部

321 阅读1分钟

vue跳转新页面之后会停留在上个页面滑动的位置,但我们实际新页面需要回到顶部。

全部页面的话就在router文件夹的index.js中设置

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

单个页面就在当前页面的mounted里面设置

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