-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Add API to manually remove/unmount component in <keep-alive> cache #2077
Comments
This has been discussed multiple times: vuejs/vue#6259 vuejs/vue#8028 vuejs/vue#10487. This is probably worth going through an RFC by comparing existing solutions with include, exclude and max |
If include/exclude could match some unique feature of the instance (type or the instance itself), it might be sufficient, but I am not sure if that is a better solution than just explicitly remove an instance of a vue component from the cache. |
Closed, tracking here vuejs/rfcs#284 |
I used the following code, it seems ok:
|
@yuanjinyong I did the similar trying before, but the __v_cache only exposed in develop mode, can not used in production. |
Any update, hack or work around? Thanks, and by the way, Vue3 is just ✨ awesome ✨ |
I encountered the same issue when trying to implement tabs that share a same component. When a tab is closed by user, I would like to delete the cache of that particular instance only. Here is what I think would be the most elegant way to tackle this in Vue3. Since KeepAlive takes
Then, use it where you need to create the component instance, for example, in the router or a store. In my case, I am dynamically importing the component, so the code would look something like this:
Finally, generate a computed property for use as the
Now bind it to the
In the above example, when you remove an item from the |
What problem does this feature solve?
When using vue-router to navigate in a larger SPA, keep-alive is a very nice way to keep views/components that are frequently visited in the cache. When the instance of the component are cached, one avoids time and energy to reload it into memory (especially GPU related assets, which otherwise would cause unnecessary loading delay for the user).
In other words, keep-alive helps building a snappy and smooth experience for the user.
However, this also means that the components that have been visited are adding up in the memory, and can also consume some CPU even if they are never to be visited again by the user.
One can argue that the "max" property can limit this, but, I think there can be many different reasons to control this manually instead. In our specific case, we would like to control this for performance reasons (based on frequency of times the component is visited, time since last visit, how time consuming is the component to load, memory consumption of the component, etc.).
Right now we have managed to fix this with a fork, where we expose the function pruneCacheEntry(key: CacheKey) in KeepAlive.ts, but it would have been much better if this was a part vue itself.
What does the proposed API look like?
...
The text was updated successfully, but these errors were encountered: