index.vue 5.6 KB
<template>
    <el-tabs v-model="activeIndex"
             v-if="openTab.length"
             type="card"
             :closable = "tabCloseable"
             @tab-click='tabClick'
             @tab-remove="tabRemove"
             style="width: 100%">
        <el-tab-pane
                v-for="(item, index) in this.$store.state.openTab"
                :key="item.name"
                :label="item.name"
                :name="item.route"
        >

                    <section class="content-container">
                        <div class="grid-content bg-purple-light">
            <!--                <el-col :span="24" class="breadcrumb-container">-->
            <!--                    <strong class="title">{{$route.name}}</strong>-->
            <!--                    <el-breadcrumb separator="/" class="breadcrumb-inner">-->
            <!--                        <el-breadcrumb-item v-for="item in $route.matched" :key="item.path">-->
            <!--                            {{ item.name }}-->
            <!--                        </el-breadcrumb-item>-->
            <!--                    </el-breadcrumb>-->
            <!--                </el-col>-->
                            <el-col :span="24" class="content-wrapper">
                                <transition name="fade" mode="out-in">
                                    <router-view></router-view>
                                </transition>
                            </el-col>
                        </div>
                    </section>
        </el-tab-pane>
    </el-tabs>
</template>

<script>
    export default {
        name: 'TabMenu',
        data() {
            return {
                tabCloseable: true
            }
        },
        methods: {
            initTab(){
                // 刷新时以当前路由做为tab加入tabs
                // 当前路由不是首页时,添加首页以及另一页到store里,并设置激活状态
                // 当当前路由是首页时,添加首页到store,并设置激活状态
                if (this.$route.path !== '/' && this.$route.path !== '/main') {
                    // console.log('1');
                    this.$store.commit('add_tabs', {route: '/main' , name: '首页'});
                    this.$store.commit('add_tabs', {route: this.$route.path , name: this.$route.name });
                    this.$store.commit('set_active_index', this.$route.path);
                } else {
                    // console.log('2');
                    this.$store.commit('add_tabs', {route: '/main', name: '首页'});
                    this.$store.commit('set_active_index', '/main');
                    this.$router.push('/main');
                }
            },
            tabClick(tab){
                // console.log("tab",tab);
                // console.log('active',this.$store.state.activeIndex);
                this.$router.push({path: this.$store.state.activeIndex});
            },
            tabRemove(targetName){
                // console.log("tabRemove",targetName);
                //首页不删
                if(targetName == '/main'){
                    return
                }
                this.$store.commit('delete_tabs', targetName);
                if (this.$store.state.activeIndex === targetName) {
                    // 设置当前激活的路由
                    if (this.$store.state.openTab && this.$store.state.openTab.length >=1) {
                        // console.log('=============',this.$store.state.openTab[this.$store.state.openTab.length-1].route)
                        this.$store.commit('set_active_index', this.$store.state.openTab[this.$store.state.openTab.length-1].route);
                        this.$router.push({path: this.$store.state.activeIndex});
                        this.tabCloseable = false;
                    } else {
                        this.$router.push({path: '/main'});
                    }
                }
            },
        },
        mounted() {
            this.initTab();
        },
        watch:{
            '$route'(to,from){
                //判断路由是否已经打开
                //已经打开的 ,将其置为active
                //未打开的,将其放入队列里
                let flag = false;
                for(let item of this.$store.state.openTab){
                    // console.log("item.name",item.name)
                    // console.log("t0.name",to.name)

                    if(item.name === to.name){
                        // console.log('to.path',to.path);
                        this.$store.commit('set_active_index',to.path)
                        flag = true;
                        break;
                    }
                }

                if(!flag){
                    // console.log('to.path',to.path);
                    this.$store.commit('add_tabs', {route: to.path, name: to.name});
                    this.$store.commit('set_active_index', to.path);
                }

            }
        },
        computed:{
            openTab:{
                get: function () {
                    return this.$store.state.openTab;
                },
                set:function (value) {
                    this.$store.commit('set_tabs', {value});
                    // console.log("opebTab监视:value = "+ value);
                }

            },
            activeIndex:{
                get:function () {
                    return this.$store.state.activeIndex;
                },
                set:function (value) {
                    this.$store.commit('set_active_index', value);
                }

            }
        }
    }
</script>

<style>
</style>