vue回到上一个位置
作者:互联网
路由
export default new Router({ // mode: 'history', routes: [ { path: '/', name: 'Home', component: Home, meta: { keepAlive: true } } ], // 页面切换始终在最顶部 scrollBehavior (to, from, savedPosition) { console.log(savedPosition) if (savedPosition) { return savedPosition } else { return { x: 0, y: 0 } } } })
App.vue
<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>
调用的页面
// 在页面离开时记录滚动位置 beforeRouteLeave (to, from, next) { this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop next() }, // 进入该页面时,用之前保存的滚动位置赋值 beforeRouteEnter (to, from, next) { next(vm => { document.body.scrollTop = vm.scrollTop }) },
+1页
<div @click="back">
<div class="iconfont header-abs-back"></div>
</div>
back () {
this.$router.go(-1)
}
标签:vue,savedPosition,回到,位置,vm,next,scrollTop,document,页面 来源: https://www.cnblogs.com/ronle/p/10982391.html