当前位置:网站首页>vue使用keep-alive实现页面缓存
vue使用keep-alive实现页面缓存
2022-07-27 15:55:00 【V_AYA_V】
最近遇到一需求,从一个看板页面跳转到其他页面之后再回到看板页面,需要定位到跳转之前的位置。主要用到了vue的keep-alive,功能整体实现难度不大,但是总结一下实现过程中遇到的一些问题。
1.keep-alive简介
官网传送门
2.路由修改
为了区别不同路由的缓存与否,在路由配置中meta添加keepAlive字段,修改App.vue
//router.js
{
path: '/xxx',
name: 'xxx',
component: ()=>{
}
meta: {
keepAlive: true //在添加keepAlive属性
}
}
//app.vue
<!--会被缓存的路由-->
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<!--不会被缓存的路由-->
<router-view v-if="!$route.meta.keepAlive"></router-view>
3.注意事项
使用keep-alive包裹的路由会多两个钩子函数deactived(被 keep-alive 缓存的组件停用时调用),actived(被 keep-alive 缓存的组件激活时调用)。另外使用keep-alive包裹的组件只会调用一次created,mounted等钩子函数,如果在路由更改之后请求数据务必在actived钩子函数中重新请求数据。
4.缓存页面高度实践
缓存页面高度指导思想,在deactived钩子函数执行的时候缓存页面滚动条高度,在actived时动态设置页面滚动条高度。在给出的代码示例中,假设在main-container中引入了两个组件,两个组件的高度比较高,超出了一屏内容。window._.debounce使用的是lodash.js方法。由于代码结构遗留问题,页面超出显示滚动条仅显示在main-container容器中。
...
//离开页面,记录容器滚动条高度
deactivated () {
this.scrollY = this.$refs['mainDom'].scrollTop
},
//进入页面,设置容器滚动条高度
activated() {
this.$refs['mainDom'].scrollTo({
top: this.scrollY,left: 0})
}
...
误区:
页面滚动之后,使用document.getElementsByClassName获取DOM,打印为空。原因是执行到deactivated钩子时页面已经跳转,此时的document已经不再是当前页面,故获取的DOM不存在。
在deactivated后发现执行到这一步的时候this实例指向的还是当前页面,遂使用this.$refs获取,打印出来scrollTop为0(这里没有搞明白,已经滚动但是无数据,可能是dom元素产生了一些变化)
替代方案
在mounted的时候就监听滚动事件,动态的设置scrollY,在activated的时候设置上去。
mounted(){
this.$refs['mainDom'].addEventListener('scroll', window._.debounce(() => {
this.scrollY = this.$refs['mainDom'].scrollTop
},150), false)
},
追加需求
如果B路由设置了路由缓存,由B路由回到上级A路由不想缓存路由,由B路由跳转到下级C路由想缓存路由。实现功能也很简单,在路由钩子函数中动态设置meta里面的keepAlive值就OK了。
beforeRouteLeave(to, from, next) {
const {
name} = to
if(name == 'A路由名字'){
from.meta.keepAlive = false
}else{
from.meta.keepAlive = true
}
next();
}
完整代码
<template>
<div class="main-container" ref="mainDom">
<template1 ref="template1 "/>
<template2 ref="template2 "/>
</div>
</template>
<script>
import template1 from 'xxx/xxx/template1.vue'
import template2 from 'xxx/xxx/template2.vue'
export default {
components:{
template1, template2
},
beforeRouteLeave(to, from, next) {
const {
name} = to
if(name == 'A路由名字'){
from.meta.keepAlive = false
}else{
from.meta.keepAlive = true
}
next();
},
data(){
return{
scrollY:0
}
}
mounted(){
this.$refs['mainDom'].addEventListener('scroll', window._.debounce(() => {
this.scrollY = this.$refs['mainDom'].scrollTop
},150), false)
},
deactivated () {
},
activated() {
this.$refs['mainDom'].scrollTo({
top: this.scrollY,left: 0})
}
}
</script>
<style lang="scss" scoped>
.main-container{
height: 100vh;
background: #EEEEED;
overflow-y: scroll;
box-sizing: border-box;
padding-top: 0.6rem;
...
}
</style>
边栏推荐
- Understand JVM language
- ACL 2022 | prompt based automatic depolarization: effectively reducing bias in the pre training language model
- How to learn C language? This article gives you the complete answer
- X-sheet development tutorial: initialization configuration custom layout
- Initial polymorphism
- Knowledge dry goods: basic storage service novice Experience Camp
- From digitalization to intelligent operation and maintenance: what are the values and challenges?
- 【Codeforces】 A. Computer Game
- Zhengzhou University database course resource description
- 卷积神经网络——SSD论文翻译
猜你喜欢
![[MCU] 2.3 CPU of AT89S52](/img/4c/7b9d235bf8a919339d75a7ec95789f.png)
[MCU] 2.3 CPU of AT89S52

面试好难啊!蚂蚁金服的六轮面试我是强撑过来!差点OUT(面试复盘)

Compilation and testing of raspberry pie driver code

I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet

机器学习——概念理解之IoU

Personal understanding of convolution calculation process of convolution neural network

JSP custom tag (bottom)

Zhengzhou University database course resource description

知物由学 | 关联图分析在反作弊业务中的应用

Evaluation index of machine learning (II) -- classification evaluation index
随机推荐
Taishan Office Technology Lecture: word strange paragraph borders
[introduction to database system (Wang Shan)] Chapter 1 - Introduction
Application of knowing things and learning | correlation graph analysis in anti cheating business
numpy数组矩阵操作
[MCU] 2.3 CPU of AT89S52
Some suggestions for writing original technical articles
Soul 1: why is es more suitable for complex condition search than MySQL?
卷积神经网络之卷积计算过程个人理解
备份表恢复表
JDBC连接数据库读取前台无法显示数据
2022 safety officer-c certificate special operation certificate examination question bank and answers
多线程导入数据并生成错误文件用redis存储
hutool 字符串工具类
The global cloud market is growing rapidly, and data security has entered a strong regulatory era of rule of law
What are the safety risks of small games?
Establishing SSL connection without server‘s identity verification is not recommended
Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
知物由学 | 易盾移动端同构实践,几步改善官网交互体验
Knowledge dry goods: basic storage service novice Experience Camp
Mysql database defines cursor in trigger