• vue2.0 keep-alive 最佳实战(转载)


    1.基本用法

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

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

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

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

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

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

    (1)使用router.mate属性
    // 这是目前用的比较多的方式
    <keep-alive>
        <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>

    router设置

    ... 
      routes: [
        { path: '/', redirect: '/index',  component: Index, mate: { keepAlive: true }},
        {
          path: '/common',
          component: TestParent,
          children: [
            { path: '/test2', component: Test2, mate: { keepAlive: true } } 
          ]
        }
        ....
        // 表示index和test2都使用keep-alive
    (2).使用新增属性inlcude/exclude

    2.1.0后提供了include/exclude两个属性 可以针对性缓存相应的组件

    <!-- comma-delimited string -->
    <keep-alive include="a,b">
      <component :is="view"></component>
    </keep-alive>
    <!-- regex (use v-bind) -->
    <keep-alive :include="/a|b/">
      <component :is="view"></component>
    </keep-alive>
    
    //其中a,b是组件的name

    注意:这种方法都是预先知道组件的名称的

    (2)动态判断
    <keep-alive :include="includedComponents">
      <router-view></router-view>
    </keep-alive>

    includedComponents动态设置即可

  • 相关阅读:
    高精度除法(到小数点后200位)
    CodeForces-Zuhair and Strings(思维+枚举)
    Codeforces-Salem and Sticks(枚举+思维)
    idata的各个类型
    C51串口的SCON寄存器及工作…
    XCode快捷键
    Objective-C 的 self 和 super 详解 (用简单程序说明问题)
    UITextFieldDelegate委托方法注释
    Objective-C 基础语法log打印那些事儿(一)
    fcntl详细说明
  • 原文地址:https://www.cnblogs.com/daiwenru/p/7810252.html
Copyright © 2020-2023  润新知