vue3 keep-alive 二级路由遇到的问题

需求:子页面仅第一次进入时调用接口

App.vue

<router-view v-slot="{ Component, route }">
   <keep-alive :include="cacheRoutes">
      <component :is="Component" :key="route.fullPath" />
   </keep-alive>
</router-view>

二级路由 Layout.vue

<div class="app-wrapper">
    <global-menu></global-menu>
    <div class="app-wrapper__content">
      <router-view v-slot="{ Component, route }">
        <keep-alive :include="cacheRoutes">
          <component :is="Component" :key="route.fullPath" />
        </keep-alive>
      </router-view>
    </div>
    <div class="app-wrapper__footer">
      <van-tabbar
        fixed
        :route="true"
        safe-area-inset-bottom
        placeholder
      >
      </van-tabbar>
    </div>
  </div>
  • Layout不放入keepAlive include中
    • 子路由keepAlive失效,即每次跳转都调用mounted
  • Layout放入keepAlive include中
    • 子路由keepAlive有效
    • 有key的情况下,大约是Layout的关系?点回来时会触发Layout下已缓存的所有onActivated,暂时处理方法:onActivated判断一下当前路由
    • 不加key的情况下,会出现参数不一致的同一页面不刷新
    • Layout下的页面如果第一次进来时,会触发两次子页面mounted,子页面mounted→Layout页面mounted→子页面mounted

 。???

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值