vue返回上一页面时回到原先滚动的位置的方法

项目结束,测试时发现在首页商品列表中,向上滑动几页后点击进入详情,从详情页面返回商品列表时,页面回到了最顶部,测试不通过说是用户体验不好,要求从哪里点击进去返回该页面时回到原先的滚动页面。

思路:因为vue是单页面应用,进入其他页面时会销毁该页面,用keep-alive不让其刷新,具体实现为:

(1).在App.vue中加入:

<template>

<div id="app">

<!--<router-view/>-->

<!--页面返回不刷新-->

<keep-alive>

<router-view v-if="$route.meta.keepAlive"></router-view>

</keep-alive>

<router-view v-if="!$route.meta.keepAlive"></router-view>

</div>

</template>

(2).index.js页面

export default new Router({

routes: [{

path: '/',

name: 'index',

component: index,

meta: {

keepAlive: true

}

},

这样在index.vue中,mounted方发只走一次,在浏览器上实现了返回原来滚动位置的目的。但是在手机上测试,发现没用,解决手机上实现目的的方法:

//在页面离开时记录滚动位置

beforeRouteLeave (to, from, next) {

this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop

next()

},

//进入该页面时,用之前保存的滚动位置赋值

beforeRouteEnter (to, from, next) {

next(vm => {

document.body.scrollTop = vm.scrollTop

})

},

OK!实现!!

以上是 vue返回上一页面时回到原先滚动的位置的方法 的全部内容, 来源链接: utcz.com/z/344451.html

回到顶部