I had some logic in the mounted()
method before I started using <keep-alive>
. For instance: document.title = this.title
. Now the title changes upon the first load only.
I could listen for route changes, but it won't work when you land on the page.
What is the proper way to detect loading of another component in <keep-alive>
?
Keep-Alive components are a pretty simple technique to start using. In its simplest form, all you have to do is add a wrapper element to your dynamic component. It's super useful to save state and quickly switch between dynamic components.
The Keep-Alive general header allows the sender to hint about how the connection may be used to set a timeout and a maximum amount of requests.
And among both of them, mounted hooks are also known as most used hooks because it is easily handled when there is no concept involved of “Server-side Rendering ” and created is more useful when we are dealing with servers and fetching data from backend API. It occurs at the earliest stage of the Vue lifecycle.
Move your logic to the activated
lifecycle hook, which is called whenever a keep-alive component is activated.
For example:
new Vue({
...
activated: function() {
document.title = this.title;
},
...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With