keep-alive
有时候咱们不但愿组件被从新渲染影响使用体验;或者处于性能考虑,避免屡次重复渲染下降性能。而是但愿组件能够缓存下来,维持当前的状态。这时候就须要用到keep-alive
组件。 官网释义:html
<keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。和 <transition> 类似,<keep-alive> 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出如今父组件链中。
当组件在 <keep-alive> 内被切换,它的 activated 和 deactivated 这两个生命周期钩子函数将会被对应执行。
在 2.2.0 及其更高版本中,activated 和 deactivated 将会在 <keep-alive> 树内的全部嵌套组件中触发。
主要用于保留组件状态或避免从新渲染。
复制代码
若是未使用keep-alive
组件,则在页面回退时仍然会从新渲染页面,触发created
钩子,使用体验很差。 在如下场景中使用keep-alive
组件会显著提升用户体验:vue
keep-alive
的生命周期created
> mounted
> activated
deactivated
activated
mounted
中;组件每次进去执行的方法放在 activated
中1、更改App.vue
<div id="app" class='wrapper'>
<keep-alive>
<!-- 须要缓存的视图组件 -->
<router-view v-if="$route.meta.keepAlive">
</router-view>
</keep-alive>
<!-- 不须要缓存的视图组件 -->
<router-view v-if="!$route.meta.keepAlive">
</router-view>
</div>
复制代码
2、在路由中设置keepAlive
{
path: 'list',
name: 'itemList', // 商品管理
component (resolve) {
require(['@/pages/item/list'], resolve)
},
meta: {
keepAlive: true,
title: '商品管理'
}
}
复制代码
3、更改
beforeEach
钩子
这一步是为了清空无用的页面缓存。 假设如今A、B两个页面都开启的缓存:缓存
为了解决这个问题,须要判断页面是在前进仍是后退。 在beforeEach
钩子添加代码:bash
let toDepth = to.path.split('/').length
let fromDepth = from.path.split('/').length
if (toDepth < fromDepth) {
// console.log('后退。。。')
from.meta.keepAlive = false
to.meta.keepAlive = true
}
复制代码
keep-alive
并不会记录页面的滚动位置,因此咱们在跳转时须要记录当前的滚动位置,在触发activated
钩子时从新定位到原有位置。 具体设计思路:app
deactivated
钩子中记录当前滚动位置,在这里我使用的是localStorage
:deactivated () {
window.localStorage.setItem(this.key, JSON.stringify({
listScrollTop: this.scrollTop
}))
}
复制代码
activated
钩子中滚动:this.cacheData = window.localStorage.getItem(this.key) ? JSON.parse(window.localStorage.getItem(this.key)) : null
$('.sidebar-item').scrollTop(this.cacheData.listScrollTop)
复制代码
参考连接:async