vue2 路由缓存问题
大致需求是这样的, 从A页面跳转到B页面 A缓存,其他页面则不缓存。
创建的路由,不好修改创建路由的代码逻辑, 采用了如下方式,
如果按照beforeEach
中 根据to/from
来判断设置keepAlive
, to="A"的时候设置缓存,离开设置false
, 结果依然存在缓存,
<keep-alive> <router-view v-if="$route.meta.keepAlive" :key="key" />
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" :key="key" />
怎么要合理缓存?
回答:
文档
<!-- 逗号分隔字符串 --><keep-alive include="a,b">
<component :is="view"></component>
</keep-alive>
<!-- 正则表达式 (使用 `v-bind`) -->
<keep-alive :include="/a|b/">
<component :is="view"></component>
</keep-alive>
<!-- 数组 (使用 `v-bind`) -->
<keep-alive :include="['a', 'b']">
<component :is="view"></component>
</keep-alive>
以上是 vue2 路由缓存问题 的全部内容, 来源链接: utcz.com/p/935383.html