首先这个交互我也叫不出专业的名字,大体场景是这样的:vue
SomePage -> List: List从新加载
List -> Info -> List: List使用缓存
复制代码
SomePage -> Form: Form从新加载
Form -> Address -> Form: Form使用缓存
复制代码
以上是简单举个例子,实际上这相似我在上家公司作移动端(SPA)时碰到的问题,只不过咱们是商家列表,但交互相似,刚开始我是直接用路由方式解决的,相似将Info做为List的子页面,将Address做为Form的子页面。实现是实现了,但太麻烦了,维护麻烦啊😂,而且对组内其它伙伴不太友好。直到某一次我决定对其进行重构,重构前也没有什么好思路,而后Google了一番茅塞顿开:利用keep-alive的include实现,当时知道了思路,我就没往下看了,本身动手丰衣足食😁。node
统一一下词汇,下文中说的类列表页
就是List,类详情页
就是Infowebpack
刚开始我还踩了一个坑,主要是Vue的keep-alive当时不多接触,都是Copy......git
首先按照以前使用(copy)keep-alive
的套路是像这样的:github
<keep-alive>
<router-view v-if="$route.meta.keepAlive"/>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"/>
复制代码
而后我将它改形成这样web
<keep-alive>
<router-view v-if="$route.meta.keepAlive"/>
</keep-alive>
<keep-alive :include="include">
<router-view v-if="!$route.meta.keepAlive"/>
</keep-alive>
复制代码
就是控制这个include
,具体的作法是,先在类列表页的路由选项meta
中增长一个cacheTo
字段,表示对指定页面缓存,值为一个数组,而后:vue-cli
router.beforeEach((to, from, next) => {
if (isPageLikeList(from)) {
// 若是from是类列表页面
const fromCacheTo = from.meta.cacheTo
if (fromCacheTo.indexOf(to.name) > -1) {
// 若是to是类详情页面
// 将from对应的组件设置缓存
} else {
// 移除from缓存
}
}
// ...
})
复制代码
但想的很美,写完发现Bug比较严重,类列表页缓存老失效,为何呢?这就要从keep-alive的include机制提及了,因而看了一下keep-alive的源码,源码中有这么一段npm
// ...
mounted () {
this.$watch('include', val => {
pruneCache(this, name => matches(val, name))
})
this.$watch('exclude', val => {
pruneCache(this, name => !matches(val, name))
})
},
// ...
复制代码
而后继续往下找侦听include
的回调逻辑数组
function pruneCache (keepAliveInstance: any, filter: Function) {
// 对于include,filter逻辑为:include包含组件名时返回true,不然返回false
const { cache, keys, _vnode } = keepAliveInstance
for (const key in cache) {
const cachedNode: ?VNode = cache[key]
if (cachedNode) {
const name: ?string = getComponentName(cachedNode.componentOptions)
// 若组件不在include范围中
if (name && !filter(name)) {
pruneCacheEntry(cache, key, keys, _vnode)
}
}
}
}
function pruneCacheEntry ( cache: VNodeCache, key: string, keys: Array<string>, current?: VNode ) {
const cached = cache[key]
// 销毁组件
if (cached && (!current || cached.tag !== current.tag)) {
cached.componentInstance.$destroy()
}
// 清除组件的缓存
cache[key] = null
remove(keys, key)
}
复制代码
大体流程是,include
变化后(不论是新增了仍是减小了),若是组件不在include
中而且当前的cache[组件name]
是有缓存的,就执行pruneCacheEntry,销毁组件并清除组件的缓存。
那么这对我上述的逻辑有什么影响呢?按照我以前的流程,动态控制include
,这样作删除组件缓存是没有问题,可是新增就会有问题了,由于新增操做发生在从类列表页离开进入到类详情页以前,此时类列表页已经存在了,而后源码中侦听include
的流程中也只有清除缓存的操做。
知道了问题,如何解决就很清晰了
咱们能够将对include
的操做放到类列表页进入前(router.beforeEach),这样就能有效缓存类列表页了,其实这东西就是这么简单,先无论三七二十一,只要是类列表页,进入以前都将其缓存,离开时判断新路由是不是该类列表页指定的类详情页,若是不是就清除缓存。那么在实现以前,我们先将场景再细化一下,以前简直比玩具还玩具......
SomePage -> List: List从新加载
// 第一种
List -> Info -> List: List使用缓存
// 第二种
List -> Info -> SomePage -> List: List从新加载
// 第三种,不限层级
List -> Info -> OtherList -> OtherInfo -> ... -> List: List、Info、OtherList...使用缓存
其中第三种,从OtherInfo返回OtherList时,OtherInfo应该是要被清除缓存的,依次类推。也就是说,返回时不保留以前的缓存
复制代码
如今,咱们根据例举的场景将大体逻辑肯定一下。这里有一个点是很明确的,那就是: 咱们只须要关心路由切换时的to
和from
,从这2个路由对象着手去解决。那么这里能够例举出4中状况:
to
和from
都不是类列表页to
和from
都是类列表页to
是类列表页from
是类列表页如今根据这4种状况细化一下
to
和from
都不是类列表页
to
和from
都是类列表页
to
不在from
的配置中,清空缓存,同时要新增to
缓存;from
的缓存,新增to
缓存to
是类列表页
from
不在to
的配置中,清空缓存,新增to
缓存to
不在from
的配置中,清空缓存如何判断是不是类列表页?
[
{
path: '/list',
name: 'list',
meta: {
cacheTo: ['info']
}
// ...
},
{
path: '/info',
name: 'info',
// ...
}
]
复制代码
如上,在路由中维护一个字段如cacheTo
,若是配置了组件名,就认为是类列表页面
逻辑理清楚咯,接下来具体实现,咱们将它作得通用一点(可拔插),而且尽可能保证对原项目有较小的侵入性,我将它命名为VKeepAliveChain
(缓存链)
首先,若是像踩坑记那样维护include
,不太具有可拔插的特性,我还得去搞一个store
,那么这个include
可使用Vue.observable
处理
// VKeepAliveChain.js
const state = Vue.observable({
caches: []
})
const clearCache = () => {
if (state.caches.length > 0) {
state.caches = []
}
}
const addCache = name => state.caches.push(name)
复制代码
为了不像踩坑记那样直接使用<keep-alive :include="include">
,咱们实现一个函数式组件来解决
// VKeepAliveChain.js
export const VKeepAliveChain = {
install (Vue, options = { key: 'cacheTo' }) {
const { key } = options
// 支持一下自定义key
if (key) {
cacheKey = key
}
// 直接透传children,因此会像keep-alive同样,只拿第一个组件节点
const component = {
name: 'VKeepAliveChain',
functional: true,
render (h, { children }) {
return h(
'keep-alive',
{ props: { include: state.caches } },
children
)
}
}
Vue.component('VKeepAliveChain', component)
}
}
复制代码
如今咱们来实现缓存控制的主要逻辑,因为要利用router.beforeEach
,约定了尽可能小的侵入性,这里能够merge一下
// VKeepAliveChain.js
const defaultHook = (to, from, next) => next()
export const mergeBeforeEachHook = (hook = defaultHook) => {
return (to, from, next) => {
// 缓存控制逻辑
// 1. 都不是类列表页
// 清空缓存
// 2. 都是类列表页
// 若`to`不在`from`的配置中,清空缓存,同时要新增`to`缓存
// 保留`from`的缓存,新增`to`缓存
// 3. 新路由是类列表页
// 若`from`不在`to`的配置中,清空缓存,新增`to`缓存
// 不然,无需处理
// 4. 旧路由是类列表页
// 若`to`不在`from`的配置中,清空缓存
const toName = to.name
const toCacheTo = (to.meta || {})[cacheKey]
const isToPageLikeList = toCacheTo && toCacheTo.length > 0
const fromName = from.name
const fromCacheTo = (from.meta || {})[cacheKey]
const isFromPageLikeList = fromCacheTo && fromCacheTo.length > 0
if (!isToPageLikeList && !isFromPageLikeList) {
clearCache()
} else if (isToPageLikeList && isFromPageLikeList) {
if (fromCacheTo.indexOf(toName) === -1) {
clearCache()
}
addCache(toName)
} else if (isToPageLikeList) {
if (toCacheTo.indexOf(fromName) === -1) {
clearCache()
addCache(toName)
}
} else if (isFromPageLikeList) {
if (fromCacheTo.indexOf(toName) === -1) {
clearCache()
}
}
return hook(to, from, next)
}
}
复制代码
那么整个缓存链的功能就实现了,同时我将它发不到了npm v-keep-alive-chain上。
首先引入并注册它
// main.js
import { mergeBeforeEachHook, VKeepAliveChain } from 'v-keep-alive-chain'
Vue.use(VKeepAliveChain, {
key: 'cacheTo' // 可选的 默认为cacheTo
})
// 若是你没有注册过beforeEach
router.beforeEach(mergeBeforeEachHook())
// 若是有注册beforeEach
router.beforeEach(mergeBeforeEachHook((to, from, next) => {
next()
}))
复制代码
而后在App.vue(视你的状况而定)中
<keep-alive>
<router-view v-if="$route.meta.keepAlive"/>
</keep-alive>
<VKeepAliveChain>
<router-view v-if="!$route.meta.keepAlive"/>
</VKeepAliveChain>
复制代码
接着在router中配置你的需求
[
{
path: '/list',
name: 'list',
meta: {
cacheTo: ['info']
}
// ...
},
{
path: '/info',
name: 'info',
// ...
}
]
复制代码
而后就能愉快的玩耍了
name
属性,而且这个name
须要和组件name
同样cacheTo
优先级小于keepAlive
,因此,处理这种需求的页面不要设置keepAlive
文章写的比较快,若有什么错误,能够下方留言咯
朋友,看到这里,但愿文章对你有启发,本人很是欢迎技术交流,若是你以为文章对你有用,还请给老弟一个👍,平时我是不在意这些个的,但因为我立刻要投递简历了,须要点东西撑门面,没得办法,履历太差了,谢谢你,愿生活带给你美好!!!