问题
组件切换的时候保留上次操作的信息
解决
如果我们自己写的话会用到本地缓存,然后还要监听路由的变化
vue方案
解决keep-alive 当组件使用的时候会自动保存数据 如果没有 include 会默认缓存和全部组件
使用
<keep-alive :max="10">
//keep-alive包裹住对应的组件就可以了
<component :is="view"></component>
</keep-alive>
include
使用版本
include 内容是class 里面的内容
2.1.0+
<!-- 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>
<!-- Array (use `v-bind`) -->
<keep-alive :include="['a', 'b']">
<component :is="view"></component>
</keep-alive
官网链接