关于使用vant的tab切换结合动态组件:is后出现生命周期叠加——生命周期方法多次调用的问题

关于使用vant的tab切换加动态组件:is后出现生命周期叠加——生命周期方法多次调用的问题

原代码

 <van-tabs v-model:active="active" swipeable @change="tabChange">
      <van-tab v-for="(item, index) in tabs" :key="index">
        <template #title>{{ item.title }} </template>
        <component
          :is="componentShow"
          :detail="detail"
          :loading="loading"
          @updateDetail="updateDetail"
          @changeActiveComponent="changeActiveComponent"
        ></component>
      </van-tab>
    </van-tabs>

 

 所以判断为组件没有被销毁,生命周期没有被及时释放导致

 起初为了不耽误开发进度,直接不使用动态组件

 <van-tabs v-model:active="active" :swipeable="active != 4">
      <van-tab>
        <template #title>舆情 </template>
        <Component0
          :detail="detail"
          :loading="loading"
          @updateDetail="updateDetail"
          @changeActiveComponent="changeActiveComponent"
        ></Component0>
      </van-tab>
      <van-tab>
        <template #title>处置日志 </template>
        <HandleLog :detail="detail"></HandleLog>
      </van-tab>
      <van-tab>
        <template #title>审批 </template>
        <Approval :detail="detail"></Approval>
      </van-tab>
      <van-tab>
        <template #title>脉络 </template>
        <Venation :detail="detail"></Venation>
      </van-tab>
      <van-tab>
        <template #title>进程 </template>
        <Process :detail="detail"></Process>
      </van-tab>
      <van-tab>
        <template #title>事实情况 </template>
        <Fact :detail="detail"></Fact>
      </van-tab>
    </van-tabs>

后续进行优化改进发现了导致问题的原因

下方是改进后的代码,把动态组件位置从tab切换组件里面改为外面,成功解决问题

 

   <van-tabs v-model:active="active" swipeable @change="tabChange">
      <van-tab v-for="(item, index) in tabs" :key="index">
        <template #title>{{ item.title }} </template>
      </van-tab>
    </van-tabs>
    <component
      :is="componentShow"
      :detail="detail"
      :loading="loading"
      @updateDetail="updateDetail"
      @changeActiveComponent="changeActiveComponent"
    ></component>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JianZhen✓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值