转自:
1.基本用法
vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗
有时候 可能需要缓存整个站点的所有页面,而页面一般一进去都要触发请求的
在使用keep-alive的情况下将首次触发请求写在created钩子函数中,就能实现缓存,
比如列表页,去了详情页 回来,还是在原来的页面2.缓存部分页面或者组件
(1)使用router. meta属性
// 这是目前用的比较多的方式
router设置
... routes: [{ path: '/', redirect: '/index', component: Index, meta: { keepAlive: true }},{path: '/common',component: TestParent,children: [{ path: '/test2', component: Test2, meta: { keepAlive: true } } ]}....
// 表示index和test2都使用keep-alive
(2).使用新增属性inlcude/exclude2.1.0后提供了include/exclude两个属性 可以针对性缓存相应的组件
//其中a,b是组件的name
注意:这种方法都是预先知道组件的名称的(3)动态判断
includedComponents动态设置即可