Home > Web Front-end > JS Tutorial > Detailed explanation of the use of keep-alive cache function in vue

Detailed explanation of the use of keep-alive cache function in vue

php中世界最好的语言
Release: 2018-05-09 10:00:52
Original
2980 people have browsed it

This time I will bring you a detailed explanation of the use of the keep-alivecache function in vue. What are the precautions for using the keep-alive cache function in vue. The following is a practical case. Let’s take a look.

When we are developing a vue project, it is inevitable that the component data will be reloaded after routing switches to other components and then returns. To deal with this situation, we need to use keep-alive caches vue component information so that it will not be reloaded.

1. In app.vue

<keep-alive>
  <router-view></router-view>
</keep-alive>
Copy after login

But in this case, all components will be cached, and single component caching cannot be achieved. Effect.

Then we add the grouping files. The implementation method is as follows:

In app.vue

<!--这里是需要keepalive的-->
<keep-alive>
  <router-view v-if="$route.meta.keepAlive"></router-view>
<keep-alive>
<!-- 这里不会被keepAlive -->
<router-view v-if="!$route.meta.keepAlive"></router-view>
Copy after login

2. In the routing index.js page

{
  path: '',
  name: '',
  component: '',
  meta: {keepAlive: true}   // 这个是需要keepalive的
},
{
  path: '',
  name: '',
  component: ,
  meta: {keepAlive: false}  // 这是不会被keepalive的
}
Copy after login

This implements the caching function of some components

If the cached component wants to clear the data or execute the initialization method, in When loading the component, call the activated hook function , as follows:

activated: function () {
  this.data = ‘'
}
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How does JS store original values ​​and reference values

Detailed explanation of the use of common JS functions

The above is the detailed content of Detailed explanation of the use of keep-alive cache function in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template