参考:https://blog.csdn.net/qq_40204835/article/details/79853685

方法一: 利用Keep-Alive和监听器

1.首先在路由中引入需要的模块

{
path: ‘/scrollDemo’,
name: ‘scrollDemo’,
meta: {
keepAlive: true // 需要缓存
},
component: resolve => { require([‘../view/scrollDemo.vue’],
resolve) }
}

2.在App.vue中设置缓存组件

 <keep-alive>   // 缓存组件跳转的页面
  <router-view v-if="$route.meta.keepAlive" class="ui-view" transition-mode="out-in"></router-view>
</keep-alive>
// 非缓存组件跳转页面
<router-view v-if="!$route.meta.keepAlive" class="ui-view" transition-mode="out-in"></router-view>

3.在页面注册对应的事件

(1). 在data中定义一个初始值 scroll

(2). 在mouted中 ,mouted中的方法代表dom已经加载完毕

window.addEventListener('scroll', this.handleScroll);

(3).methods 用于存放页面函数

handleScroll () {
this.scroll = document.documentElement && document.documentElement.scrollTop
console.log(this.scroll)
}

4.activated 为keep-alive加载时调用

activated() {
if(this.scroll > 0){
window.scrollTo(0, this.scroll);
this.scroll = 0;
window.addEventListener('scroll', this.handleScroll);
}
}

5.deactivated 页面退出时关闭事件 防止其他页面出现问题

deactivated(){
window.removeEventListener('scroll', this.handleScroll);
}

方法二:利用beforeRouteLeave和watch

main.js中:

var store = new Vuex.Store({   //记得先引入vuex
state: {
recruitScrollY: 0
},
getters: {
recruitScrollY: state => state.recruitScrollY
},
mutations: {
changeRecruitScrollY(state, recruitScrollY) {
state.recruitScrollY = recruitScrollY;
}
},
actions: { },
modules: {}
})

组件中(/flashSaleListX为当前组件,即需要记住滚动条位置的组件):

methods:{
isTabRoute: function() {
if (this.$route.path === '/flashSaleListX') {
let recruitScrollY = this.$store.state.recruitScrollY
document.documentElement.scrollTop = recruitScrollY;
}
}
},
watch: {
'$route': 'isTabRoute',
},
beforeRouteLeave(to, from, next) {
let position = document.documentElement && document.documentElement.scrollTop; //记录离开页面时的位置 if (position == null) position = 0
this.$store.commit('changeRecruitScrollY', position) //离开路由时把位置存起来
next()
}

方法三:(适用于方法二获取不到滚动位置)

组件中:

<template>
<div ref="div1">
  ··· 内容···
</div>
</template>
beforeRouteEnter(to, from, next) {
next(vm => {
const div1 = vm.$refs.div1
// 记录滚动高度
div1.scrollTop = vm.scroll
})
},
beforeRouteLeave(to, from, next) {
const div1 = this.$refs.div1;
this.scroll = div1.scrollTop; //data中记得定义变量scroll
next()
}

 注:在路由配置中,记住滚动的页面keep-alive需为true

vue 路由跳转记住滚动位置,返回时回到上次滚动位置的更多相关文章

  1. vue 路由跳转记住当前页面位置

    从列表页面跳去详情页面, 在列表页面的生命周期:deactivated  中把当前的scrollTop位置存下来,可以存在localstorage中,也可以存在vuex中, 从详情页面返回列表页面:a ...

  2. vue路由跳转的方式

    vue路由跳转有四种方式 1. router-link 2. this.$router.push() (函数里面调用) 3. this.$router.replace() (用法同push) 4. t ...

  3. 详解vue 路由跳转四种方式 (带参数)

    详解vue 路由跳转四种方式 (带参数):https://www.jb51.net/article/160401.htm 1.  router-link ? 1 2 3 4 5 6 7 8 9 10 ...

  4. vue路由跳转报错解决

    vue路由跳转: setTimeout(function () { console.log(this); this.$router.push("/login"); },800) 语 ...

  5. 去除vue路由跳转地址栏后的哈希值#

    去除vue路由跳转地址栏后的哈希值#,我们只需要在路由跳转的管理文件router目录下的index.js中加上一句代码即可去掉哈希值# mode:"history" import ...

  6. Vue路由跳转到新页面时 默认在页面最底部 而不是最顶部 的解决

    今天碰到一个问题   vue路由跳转到新的页面时会直接显示页面最底部  正常情况下是显示的最顶部的  而且好多路由中不是全部都是这种情况  折腾好长时间也没解决  最后在网上找到了解决办法 其实原理很 ...

  7. vue路由跳转取消上个页面的请求和去掉重复请求

    vue路由跳转取消上个页面的请求和去掉重复请求 axios 的二次封装(拦截重复请求.异常统一处理) axios里面拦截重复请求

  8. PhpStorm 回到上次编辑位置的快捷键

    回到上次编辑位置 Ctrl + Alt + <- (向后) Ctrl + Alt + -> (向前) 这个快捷键有时和电脑桌面快捷键冲突.解决办法: win + D 回到电脑桌面,右键-& ...

  9. vue路由跳转时判断用户是否登录功能

    通过判断该用户是否登录过,如果没有登录则跳转到login登录路由,如果登录则正常跳转. 一丶首先在用户登录前后分别给出一个状态来标识此用户是否登录(建议用vuex): 简单用vuex表示一下,不会可以 ...

随机推荐

  1. postman报InvalidArgumentException

    解决方案:

  2. CSIC_716_20191111【函数对象、名称空间、作用域、global 和nonlocal】

    函数名是可以被引用,传递的是函数的内存地址.函数名赋值给变量后,只需要在变量后加上括号即可调用函数. 名称空间 内置名称空间:在python解释器中提前定义完的名字 全局名称空间:if.while.f ...

  3. CF774L Bars

    题意:给你一个二进制表示是否可以吃巧克力.一共有k个巧克力,第一天和最后一天必须吃.最小化每两次吃巧克力的最大间隔? 标程: #include<bits/stdc++.h> using n ...

  4. 阿里云Aliplayer高级功能介绍(一):视频截图

    基本介绍 H5 Video是不提供截图的API的, 视频截图需要借助Canvas,通过Canvas提供的drawImage方法,把Video的当前画面渲染到画布上, 最终通过toDataURL方法可以 ...

  5. csp-s模拟9697题解

    题面:https://www.cnblogs.com/Juve/articles/11790223.html 96: 刚一看以为是水题,直接等差数列求和就好了,然后发现模数不是质数,还要1e18*1e ...

  6. identifier of an instance of xx.entity was altered from xxKey@249e3cb2 to xxKey@74e8f4a3; nested exception is org.hibernate.HibernateException: identifier of an instance of xxentity was altered from错误

    用entityManager保存数据时报错如下 identifier of an instance of xx.entity was altered from xxKey@249e3cb2 to xx ...

  7. [VS2008] Debug版本程序发布后 由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题

    转自VC错误:http://www.vcerror.com/?p=59 问题描述: [VS2008] 版本程序发布后,运行程序弹出错误框: 由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序 ...

  8. linux大神

    http://blog.csdn.net/skykingf/article/category/780616

  9. 新增加的html里的标签元素,该元素作为事件,js获取不到id的问题

    if(flag==0){                var p = document.createElement("p");                  var text ...

  10. mysql简单的操作

    启动数据库服务     net start mysql     停止数据库服务     net stop mysql      退出数据库      exit     保存操作及结果 将在命令行窗口中 ...