【vue2】切换页面之后滚动条停留在上个页面的位置,解决方案

最近在写vue2 项目时跳转页面后滚动条的位置居然没变,就是原页面滚动条什么位置跳转后还在什么位置。正常来说跳转页面滚动条应回到顶部。百度找到解决方法之后将自己的经验总结下来

history 路由解决方法

在router/index.js中修改

const router = new Router({
    mode: "history",
    routes: routes,
	//增加这个配置
    scrollBehavior(to, from, savedPosition) {
        console.log(savedPosition)
        if(savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
});

其他路由hash遇到会完善文章

参考文章:vue2切换页面之后滚动条停留在上个页面的位置,解决方案

猜你喜欢

转载自blog.csdn.net/qq_46123200/article/details/133309534
今日推荐