Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue router keep-alive and mounted behavior

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>?

like image 259
mt.andi Avatar asked Oct 27 '17 11:10

mt.andi


People also ask

How do you use keep-alive in Vue?

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.

What is the purpose of keep-alive tag?

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.

What is the difference between mounted and created in Vue?

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.


1 Answers

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;
  },
  ...
});
like image 113
Jordan Lev Avatar answered Sep 19 '22 02:09

Jordan Lev