Home  >  Article  >  Web Front-end  >  How to use the keep-alive cache function

How to use the keep-alive cache function

php中世界最好的语言
php中世界最好的语言Original
2018-05-03 15:34:501971browse

This time I will bring you the keep-alivecachehow to use the keep-alive cache function, what are the notes for using the keep-alive cache function, the following is a practical case, let’s take a look .

Vue implements caching of component information

When we develop a vue project, we cannot avoid switching to other components in routing The component data will be reloaded after returning. To deal with this situation, we need to use keep-alive to cache the vue component information so that it will not be reloaded.

1. In app.vue

<keep-alive>
  <router-view></router-view>
</keep-alive>

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>

2. In the routing index.js page

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

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 = ‘'
}

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:

The use and difference between computed and methods in Vue.js

Detailed explanation of the steps to use js-cookie

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

Statement:
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