-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<keep-alive> doesn't destroy
cached components after when max is 1
#9972
Comments
destroy
cached components after max
has been reacheddestroy
cached components after when max is 1
It seems not to destroy components when the max is one (in which case using keep-alive is pointless btw). I removed Vue Router from the repro to make it simpler: https://jsfiddle.net/p0te9vL3/ (max is set to 2 to show it works) I marking this as a bug for consistency. The workaround would be not using a |
should we just validate max value just like this? if (parseInt(this.max) > 1) {
var ref$1 = this;
var cache = ref$1.cache;
var keys = ref$1.keys;
var key = vnode.key == null
// same constructor may get registered as different local components
// so cid alone is not enough (#3269)
? componentOptions.Ctor.cid + (componentOptions.tag ? ("::" + (componentOptions.tag)) : '')
: vnode.key;
if (cache[key]) {
vnode.componentInstance = cache[key].componentInstance;
// make current key freshest
remove(keys, key);
keys.push(key);
} else {
cache[key] = vnode;
keys.push(key);
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode);
}
}
vnode.data.keepAlive = true;
} |
@posva maybe I'm confused by the prop, but when I used |
it will cache 1 component, so as soon as you switch the cache is occupied by the new component and the previous component is removed. A cache of one entry here is the same as not having any cache |
@posva ah that makes more sense, thanks! |
Yes, in my code i delay to call |
@posva I am using keep-alive to cache some expensive page, like the following.
However, I find out that there is no "beforeDestroy" hook for the cached file. |
I wonder if can clean up nodes thoroughly.... |
|
@posva I wonder if there are any other choices for my situation. |
It may cause other problem, such as last router will fresh when activated |
Version
2.6.10
Reproduction link
https://jsfiddle.net/p0te9vL3/
Steps to reproduce
What is expected?
I'd expect once the
max
cache is reached (in the example case of 1), the component that gets pruned also gets destroyed. In the documentation it states (emphasis mine):What is actually happening?
The pruned component is never destroyed, it's just removed from the cache. In the example, the
beforeDestroy
hook is never called.Related code:
vue/src/core/components/keep-alive.js
Lines 37 to 49 in 636c9b4
The text was updated successfully, but these errors were encountered: