vue3+router4 keepalive缓存页面

需求

在列表页面上点击查看详情,跳转到一个新的页面,然后返回的时候想返回到跳转时候的页面。

实现思路

1.vuex中存在一个保存需要缓存的页面name值,当页面跳转j进来的时候执行beforeRouteEnter中往vuex中存储这个值,当页面离开的时候也就是beforeRouteLeave中判断你进入的页面是不是详情页,如果是则保留vuex中的值,如果不是则清空值。
2.还存在一种情况就是,我从列表页跳到了详情页,此时vuex中存在了这个值,但是我从详情页去另一个页面而没有回到列表页,这个时候我们就需要去清空vuex中的这个值了。
3.keepalive需要用到动态组件include去判断加载情况

具体代码如下

新建routerCache.js文件用来存储值

// 记录缓存的路由

export const routerCache = {
  state: {
    KEEP_ALIVE_COMS: []
  },
  mutations: {
    // 添加缓存组件
    ADD_KEEP_ALIVE_COM(state, name) {
      if (!state.KEEP_ALIVE_COMS.includes(name)) {
        state.KEEP_ALIVE_COMS.push(name)
      }
    },

    // 删除缓存组件
    REMOVE_KEEP_ALIVE_COM(state, name) {
      if (state.KEEP_ALIVE_COMS.includes(name)) {
        state.KEEP_ALIVE_COMS = state.KEEP_ALIVE_COMS.filter(item => item !== name)
      }
      // state.KEEP_ALIVE_COMS.pop()
    },

    // 重置缓存组件
    RESET_KEEP_ALIVE_COM(state) {
      state.KEEP_ALIVE_COMS = []
    }
  },
}

vuex中引入这个模块

import {
   
    createStore
} from 'vuex'
import {
   
    routerCache
} from 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ant Design Vue 中的 Tabs 组件可以与 Vue Router 配合使用,实现标签栏的功能。具体步骤如下: 1. 在路由配置中,添加 `meta` 字段用于标识当前路由是否需要在标签栏中显示,例如: ```javascript const routes = [ { path: '/', name: 'Home', component: Home, meta: { title: '首页', keepAlive: true, // 是否缓存组件 showTab: true, // 是否在标签栏中显示 }, }, // ... ]; ``` 2. 在 App.vue 中,使用 Tabs 组件来渲染标签栏,并使用 `router-view` 组件来渲染当前路由对应的组件: ```html <template> <div> <a-tabs v-model:selectedTabKey="selectedTabKey" type="editable-card" hide-add @edit="handleTabEdit" style="margin: 0 24px;"> <a-tab-pane v-for="(tab, index) in tabs" :key="tab.key" :tab="tab.title" :closable="index !== 0" @close="handleTabClose(index)"> </a-tab-pane> </a-tabs> <router-view /> </div> </template> <script> export default { data() { return { selectedTabKey: '/', tabs: [ { key: '/', title: '首页', }, ], }; }, created() { const { path, meta } = this.$route; if (meta.showTab) { this.selectedTabKey = path; this.addTab(path, meta.title); } }, methods: { addTab(key, title) { const index = this.tabs.findIndex((tab) => tab.key === key); if (index === -1) { this.tabs.push({ key, title, }); } }, removeTab(key) { const index = this.tabs.findIndex((tab) => tab.key === key); if (index !== -1) { this.tabs.splice(index, 1); } }, handleTabClose(index) { const { key } = this.tabs[index]; this.removeTab(key); this.$router.replace(this.tabs[this.tabs.length - 1].key); }, handleTabEdit(targetKey, action) { if (action === 'add') { this.$router.push(targetKey); } else if (action === 'remove') { this.handleTabClose(this.tabs.findIndex((tab) => tab.key === targetKey)); } }, }, }; </script> ``` 在这个示例中,我们使用了 `selectedTabKey` 属性来绑定当前选中的标签页,使用 `tabs` 数组来存储所有已打开的标签页。在 `created` 钩子函数中,我们通过判断当前路由的 `meta.showTab` 字段来决定是否需要添加标签页。在 `addTab` 方法中,我们使用 `tabs` 数组来存储已打开的标签页,防止重复添加。在 `removeTab` 方法中,我们使用 `tabs` 数组来删除已关闭的标签页。在 `handleTabEdit` 方法中,我们通过判断用户的操作来决定是添加标签页还是关闭标签页。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lbchenxy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值