路由
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)}
杂七杂八
// 在页面离开时记录滚动位置 beforeRouteLeave (to, from, next) { this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop next() }, // 进入该页面时,用之前保存的滚动位置赋值 beforeRouteEnter (to, from, next) { console.log(document.body.scrollTop) if (window.performance) { console.info(‘window.performance works fine on this browser‘) } if (performance.navigation.type === 1) { console.info(‘This page is reloaded‘) next() } else { next(vm => { document.body.scrollTop = vm.scrollTop }) } },
原文地址:https://www.cnblogs.com/ronle/p/10982391.html
时间: 2024-10-13 15:07:17