局部方法
beforeRouteLeave(to, from, next) {
const that = this
if (to.path === "/index") { //需要缓存的页面
from.meta.keepAlive = true
} else {
// from.meta.keepAlive = false
const vnode = that.$vnode
const parentVnode = vnode && vnode.parent
if (
parentVnode &&
parentVnode.componentInstance &&
parentVnode.componentInstance.cache
) {
var key =
vnode.key == null
? vnode.componentOptions.Ctor.cid +
(vnode.componentOptions.tag
? `::${vnode.componentOptions.tag}`
: '')
: vnode.key
var cache = parentVnode.componentInstance.cache
var keys = parentVnode.componentInstance.keys
if (cache[key]) {
that.$destroy()
// remove key
if (keys.length) {
var index = keys.indexOf(key)
if (index > -1) {
keys.splice(index, 1)
}
}
cache[key] = null
}
}
}
next()
}