Vue实现顶部的历史记录标签栏(后续新增标签栏右击快速关闭的效果)

快速关闭功能见此页

https://blog.csdn.net/m0_74149462/article/details/128742833?spm=1001.2014.3001.5502

默认展示首页,且首页一直固定在左侧

store/module/historyModule.js(注意文件路径,根据实际情况修改代码)

const SET_TABSVIEW = 'SET_TABSVIEW'
const DEL_TABSVIEW = 'DEL_TABSVIEW'
export default {
    state: {
        visitedTabsView: [
            {
                name: "首页",
                path: "/home"
            }
        ], // 保证每次刷新都能有最原始的一个首页的标签
    },
    mutations: {
        [SET_TABSVIEW](state: any, view: any) {
            if (state.visitedTabsView.find((n: any) => n.path === view.path)) {
                return
            }
            state.visitedTabsView.push({name: view.meta.title, path: view.path})
        },
        [DEL_TABSVIEW](state: any, view: any) {
            state.visitedTabsView.forEach((item: any, index: any) => {
                if (item.name === view.name && item.path === view.path) state.visitedTabsView.splice(index, 1)
            })
        }
    },
    actions: {
        // 添加一个新的tabsView
        addVisitedTabsView({commit}: any, view: any) {
            commit(SET_TABSVIEW, view)
        },
        // 关闭一个tabsView
        delVisitedTabsView({commit, state}: any, view: any) {
            return new Promise((resolve, reject) => {
                commit(DEL_TABSVIEW, view)
                resolve([...state.visitedTabsView])
            })
        }
    },
    getters: {
        visitedTabsView: (state: any) => state.visitedTabsView
    }
}

在Vue文件中使用

<!-- todo 历史记录标签,后续新增 在标签上右击实现 关闭左侧/关闭右侧/关闭全部 的功能 -->
<template>
    <div class="tabs-view-container">
        <router-link
                class="tags-view-item"
                :class="isActive(tag) ? 'active' : ''"
                v-for="(tag, index) in visitedTabsView"
                :to="tag.path"
                :key="index"
        >
            <el-tag
                    type="danger"
                    v-if="tag.name === '首页'"
                    :disable-transitions="false"
                    @close.prevent.stop="handleClose(tag)"
                    effect="plain"
            >
                {{ tag.name }}
            </el-tag>
            <el-tag
                    v-else
                    type="danger"
                    closable
                    :disable-transitions="false"
                    @close.prevent.stop="handleClose(tag)"
                    effect="plain"
            >
                {{ tag.name }}
            </el-tag>
        </router-link>
    </div>
</template>

<script>
    import {mapActions, mapGetters} from "vuex";

    export default {
        name: "historyPage",
        data() {
            return {};
        },
        created() {
            this.addTabsView();
        },
        computed: {
            ...mapGetters(["visitedTabsView"])
        },
        methods: {
            ...mapActions(["addVisitedTabsView", "delVisitedTabsView"]),
            addTabsView() {
                const route = this.generateRoute();
                if (!route) {
                    return false;
                }
                this.addVisitedTabsView(this.generateRoute());
            },
            generateRoute() {
                if (this.$route.name) {
                    return this.$route;
                }
                return false;
            },
            isActive(route) {
                return route.path === this.$route.path || route.name === this.$route.name;
            },
            handleClose(tag) {
                this.delVisitedTabsView(tag).then(tags => {
                    // 如果关闭的是当前显示的页面,就去到前一个 tab-view 页面
                    if (this.isActive(tag)) {
                        const lastTag = tags.slice(-1)[0];
                        // 前一个 tab-view 页面存在,就跳;不存在就到首页
                        if (lastTag) {
                            this.$router.push(lastTag.path);
                        } else {
                            this.$router.push("/home");
                        }
                    }
                });
            }
        },
        watch: {
            $route() {
                this.addTabsView();
            }
        }
    };
</script>

<style lang="scss" scoped>
    //滚动条的宽度
    ::-webkit-scrollbar {
        width: 2px;
        height: 2px;
    }
</style>

historyPage页面的样式

// historyPage 页面的样式
.tabs-view-container {
  overflow: hidden;
  white-space: nowrap;
  padding: 5px;
  //border-bottom: 1px solid #dfdfdf;

  .tags-view-item {
    .el-tag {
      margin: 0 3px;

      &:first-child {
        margin-left: 0;
      }
    }

    &.active {
      .el-tag {
        // 选中 tag 时的颜色
        background-color: #ffb6c1;
        color: #fff;

        .el-icon-close {
          color: #fff;

        }

        &:before {
          position: relative;
          content: '';
          background: #fff;
          display: inline-block;
          width: 6px;
          height: 6px;
          border-radius: 50%;
          margin-right: 2px;
        }
      }
    }
  }
}

.tabs-view-container:hover {
  cursor: pointer;
  overflow-x: auto;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值