Home  >  Article  >  Web Front-end  >  Detailed explanation of the steps to use keep-alive in vue

Detailed explanation of the steps to use keep-alive in vue

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 13:48:572281browse

This time I will bring you a detailed explanation of the steps for using keep-alive in vue. What are the precautions for using keep-alive in vue. The following is a practical case, let's take a look.

1. The role and benefits of keep-alive

In e-commerce related projects, when we enter the list page for the first time I need to request data. When I enter the details page from the list page, I need to request the data even if the details page is not cached, and then return to the list page. At this time, we use keep-alive to cache the component to prevent secondary rendering, which will save a lot of money. performance.

2. Basic usage of keep-alive

In app.vue

<!-- 缓存所有的页面 -->
<keep-alive>
 <router-view v-if="$route.meta.keep_alive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keep_alive"></router-view>

The component content that needs to be cached is directly Add in the router:

meta: {
 keepAlive: true // true 表示需要使用缓存 false表示不需要被缓存
 }

3.keep-aliveLife cycle

##When introducing keep-alive When the page is entered for the first time, the trigger sequence of the hook is created-> mounted-> activated, and deactivated is triggered when

exits. When entering again (forward or backward), only activated is triggered.

Let’s take a look at the usage problems and solutions of keep-alive in vue

Problem description

In business development, there will be scenarios where

routing jumps but returns data that needs to be retained; vue provides keep-alive to handle

Solution

Return to the dom and prevent it from being refreshed. Wrap a layer outside the vue-view. When keep-alive is introduced, the page is entered for the first time, and the trigger sequence of the hook is created-> mounted-> activated. Deactivated is triggered when exiting. When entering again (forward or backward), only activated is triggered.

EventsMounted methods, etc., are only executed once and are placed in mounted; methods that are executed each time the component is entered are placed in activated; You can wrap keep-alive or not Through parameter configuration;

<keep-alive>
  <router-view v-if="$route.meta.keepAlive" style="min-height:100%"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" style="min-height:100%"></router-view>
//不需要刷新的路由配置里面配置 meta: {keepAlive: true}, 这个路由则显示在上面标签;
//需要刷新的路由配置里面配置 meta: {keepAlive: false}, 这个路由则显示在下面标签;
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 to implement English letter case conversion in PHP

php generates random numbers, letters or numeric letters Mixed string

The above is the detailed content of Detailed explanation of the steps to use keep-alive in vue. 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