keepalive 缓存组件

app.vue
<div id="app">
  <router-view v-slot="{ Component }">
      <keep-alive :include ="keepalive.mainAliveList">
        <component :is="Component"/>
      </keep-alive>
  </router-view>
</div>
layout.vue
<el-container class="layout">
  <Header />
  <div class="mix">
    <Side />
    <div class="main">
      <router-view v-slot="{ Component }">
        <keep-alive :include ="keepalive.list">
          <component :is="Component"/>
        </keep-alive>
      </router-view>
    </div>
  </div>
</el-container>
books-setting.vue 需要缓存的组件,router:BooksSetting
const keepalive = keepaliveList(); // store存储


需求:BooksSetting进入BookImg,从BookImg页面回到BooksSetting页面BooksSetting需要缓存
进入BooksSetting页面时先将缓存组件放到keeplive中,缓存组件,进入BookImg页面时,watch可以监听到路由,进入其他页面则将keepalive清空

watch(
  () => route.name,
  (newValue, oldValue) => {
    // 缓存后的组件,可以监听到每次的route变化 
    if (newValue === 'BooksSetting' || newValue === 'BookImg') {
      if (keepalive.list.indexOf('books-setting') < 0) {
        keepalive.setList(['books-setting']);
      }
    } else {
      keepalive.setList(['']);
    }
  },
  { immediate: true }
);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值