zoukankan      html  css  js  c++  java
  • vue中keep-alive

    vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗

    1.基本用法,缓存整个页面或组件

    <keep-alive>
        <component>
          <!-- 组件将被缓存 -->
        </component>
    </keep-alive>

    有时候 可能需要缓存整个站点的所有页面,而页面一般一进去都要触发请求的 
    在使用keep-alive的情况下

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

    将首次触发请求写在created钩子函数中,就能实现缓存, 
    比如列表页,去了详情页 回来,还是在原来的页面

    2.缓存部分页面或者组件

    (1)使用router.meta属性 
    // 这是目前用的比较多的方式

    1 <keep-alive>
    2     <router-view v-if="$route.meta.keepAlive"></router-view>
    3 </keep-alive>
    4 <router-view v-if="!$route.meta.keepAlive"></router-view>

    router设置

     1 ... 
     2 routes: [
     3   { path: '/', redirect: '/index',  component: Index, mate: { keepAlive: true }},
     4   {
     5     path: '/common',
     6     component: TestParent,
     7     children: [
     8       { path: '/test2', component: Test2, mate: { keepAlive: true } } 
     9     ]
    10   }
    11   ....
    12   // 表示index和test2都使用keep-alive

    (2).使用新增属性inlcude/exclude 
    2.1.0后提供了include/exclude两个属性 可以针对性缓存相应的组件

    1 <!-- comma-delimited string -->
    2 <keep-alive include="a,b">
    3   <component :is="view"></component>
    4 </keep-alive>
    5 <!-- regex (use v-bind) -->
    6 <keep-alive :include="/a|b/">
    7   <component :is="view"></component>
    8 </keep-alive>

    // 其中a,b是组件的name 
    注意:这种方法都是预先知道组件的名称的 
    (2)动态判断

    1 <keep-alive :include="includedComponents">
    2   <router-view></router-view>
    3 </keep-alive>

    includedComponents动态设置即可

    原文链接:https://blog.csdn.net/m0_38069630/article/details/79136501

  • 相关阅读:
    外观模式
    享元模式
    c#中的抽象类和接口
    装饰者模式
    组合模式
    适配器模式
    springboot 源码篇002## web层自动装配部分源码
    springboot 源码篇002## 自动装配原理
    springboot 源码篇 01
    shell 脚本基础 第二篇
  • 原文地址:https://www.cnblogs.com/momo798/p/9024502.html
Copyright © 2011-2022 走看看