审查视图

src/components/TabMenu/index.vue 7.2 KB
朱兆平 authored
1
<template>
朱兆平 authored
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
    <div style="width: 100%">

        <Assembly></Assembly>

        <el-tabs v-model="activeIndex"
                 v-if="openTab.length"
                 type="card"
                 :closable = "tabCloseable"
                 @tab-click='tabClick'
                 @tab-remove="tabRemove"
                 style="width: 100%;margin-top: 18px;height: 50px">
            <el-tab-pane
                    v-for="(item, index) in this.$store.state.openTab"
                    :key="item.name"
                    :label="item.name"
                    :name="item.route">
            </el-tab-pane>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
            <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>
朱兆平 authored
36 37
        </el-tabs>
    </div>
朱兆平 authored
38 39 40
</template>

<script>
朱兆平 authored
41 42
    import Assembly from "@/views/bus/Assembly";
朱兆平 authored
43 44
    export default {
        name: 'TabMenu',
朱兆平 authored
45
        components: { Assembly },
朱兆平 authored
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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});
                    } else {
                        this.$router.push({path: '/main'});
                    }
朱兆平 authored
89 90

朱兆平 authored
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                }
            },
        },
        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);
                }
朱兆平 authored
121 122 123
            },
            openTab(){
                this.openTab.length === 1 ? this.tabCloseable=false :this.tabCloseable = true;
朱兆平 authored
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            }
        },
        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>
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/*底层背景色*/
    .el-tabs__nav-scroll{
        background-color: rgb(239,243,246);
    }
/*首个内部边框*/
.el-tabs--card>.el-tabs__header .el-tabs__item:first-child{
    border: 1px solid rgba(80,109,130,.64);
}
/*其他内部边框*/
.el-tabs--card>.el-tabs__header .el-tabs__item{
    border: 1px solid rgba(80,109,130,.64);
}
/*外部大边框取消表格样式*/
.el-tabs--card>.el-tabs__header .el-tabs__nav{
    border: 0px;
}
/*黑框及内字体样式*/
.el-tabs__item{
        color:#495060;
        height: 26px;
        border: 1px solid rgba(80,109,130,.64);
        margin: 4px 3px;
        line-height: 26px;
        border-radius: 5px 5px 0 0;
        background-color: #fff;
        font-family: Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Arial,sans-serif;
}
/*    选中的黑框的样式*/
.el-tabs--card>.el-tabs__header .el-tabs__item.is-active{
    border-bottom: 1px solid rgba(80,109,130,.64);
    border-radius: 5px 5px 0 0;
    background-color: #398af1;
    color: #fff;
    border-color: #398af1;
}
    .el-tabs--card>.el-tabs__header .el-tabs__item.is-active:before{
        content: "";
        background: #fff;
        display: inline-block;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        margin-right: 8px;
    }
朱兆平 authored
195
</style>