vue中使用动态路由的方式,以及路由不缓存的几种原因。

文章介绍了在Vue项目中如何使用v-if和keep-alive指令动态判断组件是否需要缓存,包括在App.vue和路由配置文件中的设置,以及通过this.$route.meta.keepAlive进行控制。同时,提到了include和exclude选项用于更精细的缓存管理,并列举了组件缓存不生效的常见问题,如组件和路由的name属性配置不正确等。
摘要由CSDN通过智能技术生成

1.使用v-if动态判断哪些组件需要缓存

a.在App.vue组件中,这是对一级路由进行缓存判断。

<template>
  <div id="app">
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>
<script>
export default {
  name: 'App'
}
</script>
<style lang="scss">
</style>

b.在路由配置文件中,定义可被缓存的路由。(设置一级路由)

{
    path: '/',
    redirect: '/home',
    component: () => import('@/views/frame/HomeLayout'),
    meta: { title: '首页', keepAlive: false },
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/frame/Home'),
        meta: { title: '首页', keepAlive: false }
      },
      {
        path: '/work',
        name: 'Work',
        component: () => import('@/views/app/home/Work'),
        meta: { title: '工作', keepAlive: true }
      }
    ]
},
{
    path: '/login',
    name: 'Login',
    component: () => import('@/views/frame/Login'),
    meta: { title: '登录', keepAlive: false }
}

注,本文件中,登录页为一级路由,需要动态缓存的话将keepAlivue属性设置为true,并根据App.vue文件中动态判定逻辑,此路由被缓存。

c.可以看到,路由配置文件中还包括二级路由(children节点下为二级路由的配置)。(设置二级路由),二级路由挂在Homelayout组件下。下文件为Homelayout组件。

<template>
  <div class="app-content">
    <div class="layout-content">
      <keep-alive>
        <router-view v-if="$route.meta.keepAlive"></router-view>
      </keep-alive>
      <router-view v-if="!$route.meta.keepAlive"></router-view>
    </div>
    <div class="layout-footer">
      <TabBar :data="tabbars" @change="handleChange" />
    </div>
  </div>
</template>

<script>
import TabBar from '@/components/TabBar'

export default {
  name: 'AppLayout',
  data() {
    return {
      tabbars: [
        {
          title: '首页',
          to: {
            name: 'Home'
          },
          icon: 'home-o'
        },
        {
          title: '个人中心',
          to: {
            name: 'User'
          },
          icon: 'user-o'
        }
      ]
    }
  },
  components: {
    TabBar
  },
  methods: {
    handleChange(v) {
      console.log('tab value:', v)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-content{
  height: calc(100% - 50px)
}
.layout-content{
  height: 100%
}
</style>

注:可以看到,此组件中,同样做了路由判断,来判断组件是否需要被缓存。

2.此方式动态控制路由缓存方法

在路由跳转时,若要缓存,则加上this.$route.meta.keepAlive = true,即可将跳转后的界面进行缓存,若不缓存即为this.$route.meta.keepAlive = false

this.$router.push(item.path)
this.$route.meta.keepAlive = true

3.另一种控制缓存方式(include好用)

 在全局状态中增加include状态,动态添加或删除即可实现控制缓存 

include - 字符串或正则表达式。只有名称匹配的组件会被缓存。
exclude - 字符串或正则表达式。任何名称匹配的组件都不会被缓存。
max - 数字。最多可以缓存多少组件实例。

由此,可以引出一个问题,组件缓存不生效的几种可能原因

1. 组件名称没有配置

组件中如果需要缓存,name属性一并要配置并且严格按照组件命名规范。

2.路由文件配置中name属性没有配置,或者与组件name名称不一致。

3.对应路由级别配置错误,如果为二级路由,只在一级组建中配置缓存keep-alive是不行的,还需要在对应的父组件中配置对应的keep-alive。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李飞要当大神

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

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

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

打赏作者

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

抵扣说明:

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

余额充值