vue 路由跳转记住滚动位置,返回时回到上次滚动位置
参考: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 路由跳转记住滚动位置,返回时回到上次滚动位置的更多相关文章
- vue 路由跳转记住当前页面位置
从列表页面跳去详情页面, 在列表页面的生命周期:deactivated 中把当前的scrollTop位置存下来,可以存在localstorage中,也可以存在vuex中, 从详情页面返回列表页面:a ...
- vue路由跳转的方式
vue路由跳转有四种方式 1. router-link 2. this.$router.push() (函数里面调用) 3. this.$router.replace() (用法同push) 4. t ...
- 详解vue 路由跳转四种方式 (带参数)
详解vue 路由跳转四种方式 (带参数):https://www.jb51.net/article/160401.htm 1. router-link ? 1 2 3 4 5 6 7 8 9 10 ...
- vue路由跳转报错解决
vue路由跳转: setTimeout(function () { console.log(this); this.$router.push("/login"); },800) 语 ...
- 去除vue路由跳转地址栏后的哈希值#
去除vue路由跳转地址栏后的哈希值#,我们只需要在路由跳转的管理文件router目录下的index.js中加上一句代码即可去掉哈希值# mode:"history" import ...
- Vue路由跳转到新页面时 默认在页面最底部 而不是最顶部 的解决
今天碰到一个问题 vue路由跳转到新的页面时会直接显示页面最底部 正常情况下是显示的最顶部的 而且好多路由中不是全部都是这种情况 折腾好长时间也没解决 最后在网上找到了解决办法 其实原理很 ...
- vue路由跳转取消上个页面的请求和去掉重复请求
vue路由跳转取消上个页面的请求和去掉重复请求 axios 的二次封装(拦截重复请求.异常统一处理) axios里面拦截重复请求
- PhpStorm 回到上次编辑位置的快捷键
回到上次编辑位置 Ctrl + Alt + <- (向后) Ctrl + Alt + -> (向前) 这个快捷键有时和电脑桌面快捷键冲突.解决办法: win + D 回到电脑桌面,右键-& ...
- vue路由跳转时判断用户是否登录功能
通过判断该用户是否登录过,如果没有登录则跳转到login登录路由,如果登录则正常跳转. 一丶首先在用户登录前后分别给出一个状态来标识此用户是否登录(建议用vuex): 简单用vuex表示一下,不会可以 ...
随机推荐
- LeetCode第九题—— Palindrome Number(判断回文数)
题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- LUOGU P2564 [SCOI2009]生日礼物 (队列+模拟)
传送门 解题思路 还是比较好想的,用一个队列,然后把所有点放在一起排个序,依次入队.每次检查队头元素的种类是否为当前入队元素种类,是的话就一直\(pop\),每次更新答案即可. 代码 #include ...
- 多线程的基本概念和Delphi线程对象Tthread介绍
多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...
- python paramiko模块学习分享
python paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Sola ...
- C#winfrom listview 设置显示图片
ListView控件有5种显示图片方式:LargeIcon(大图标),Detail(详细),SmallIcon(小图标),List(列表),Tile,常用前4种. 这里说一下设置方式:LargeIc ...
- ssm下使用分页插件PageHelper进行分页
1. 导入maven依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...
- hdu-1394(线段树求最小逆序数)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意: 给定一个n,然后又n个数字,首先,这些数字的大小是从0开始到n-1,比如样例n=10,则这十个数就 ...
- “fixed+relative≈≈absolute”——对BFC的再次思考
好久没写博客了,刚好今天跨年夜没约到什么妹子,在家宅着不如写点东西好了. 需求 昨天晚上,给公司年会做一个移动端的投票页面,遇到一个UI优化的问题: · 正文内容少于一屏时,投票提交按钮固定显示在页面 ...
- Python中好用的模块们
目录 Python中好用的模块们 datetime模块 subprocess模块 matplotlib折线图 importlib模块 Python中好用的模块们 datetime模块 相信我们都使 ...
- 第四周课堂笔记4th
编码 Ascii美国 一个字节表示一个字符,必能表示汉子 大写字母65-90 小写字母97-122 265个位置 8位表示一个字节, 8bit=1byte GBK 中国 只包含本国文字 ...