Home  >  Article  >  Web Front-end  >  Use vue’s keep-alive to improve page caching effect

Use vue’s keep-alive to improve page caching effect

WBOY
WBOYOriginal
2023-07-22 09:54:33626browse

Use Vue’s keep-alive to improve page caching effect

In the process of developing web applications, we often encounter the problem of page switching, especially in scenarios involving frequent switching, reloading every time Pages can cause performance degradation. To solve this problem, Vue provides a component called keep-alive for caching components or routes that have been rendered.

keep-alive is an abstract component that comes with Vue. It can be wrapped around the component that needs to be cached. When the component is switched, the component instance will not be destroyed, but will be cached so that Reuse next time.

Using keep-alive is very simple, just add the keep-alive tag to the component that needs to be cached. The following is a simple example to demonstrate how to use keep-alive to improve page caching effect.

First, we create a simple Vue instance and define two components: Home and About.

<template>
  <div>
    <router-link to="/">Home</router-link>
    <router-link to="/about">About</router-link>
    <keep-alive>
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

<script>
import Home from './components/Home.vue'
import About from './components/About.vue'

export default {
  components: {
    Home,
    About
  }
}
</script>

In the above code, we use Vue Router to implement route switching. Two navigation links are implemented through the router-link tag, pointing to the Home and About components respectively. In the keep-alive tag, we use Vue's dynamic component router-view to render the currently activated component and wrap it in the keep-alive component.

Next, we write the template and script code for the Home and About components respectively.

<!-- Home.vue -->
<template>
  <div>
    <h1>Home</h1>
    <p>这是Home组件</p>
  </div>
</template>

<script>
export default {
  activated() {
    console.log('Home组件被激活')
  },
  deactivated() {
    console.log('Home组件被禁用')
  }
}
</script>

<!-- About.vue -->
<template>
  <div>
    <h1>About</h1>
    <p>这是About组件</p>
  </div>
</template>

<script>
export default {
  activated() {
    console.log('About组件被激活')
  },
  deactivated() {
    console.log('About组件被禁用')
  }
}
</script>

In the Home and About components, we listen to the activation and deactivation events of the components through the life cycle hook functions activated and deactivated provided by Vue, and print relevant information on the console.

Finally, we need to create a configuration file containing routing information and introduce it into the Vue instance.

// router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]

const router = new VueRouter({
  routes
})

export default router

Now, we can run the application and switch pages. You can observe in the console that when switching components, the activated and deactivated life cycle hook functions are triggered.

By using the keep-alive component, we can see that when switching components, the cached components will not be destroyed, but will be reused directly. This avoids repeated rendering and initialization processes, greatly improving page loading speed and performance.

Summary:
Using Vue's keep-alive component can effectively improve the page caching effect, especially in frequent switching scenarios. Through a simple code example, we can learn how to use keep-alive to cache rendered components. Keep-alive is a very useful tool for optimizing the performance and user experience of web applications.

The above is the detailed content of Use vue’s keep-alive to improve page caching effect. 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