审查视图

src/components/TabMenu/index.vue 7.5 KB
1
<template>
朱兆平 authored
2 3
    <div style="width: 100%;">
<!--        <Assembly></Assembly>-->
4 5 6 7 8 9
        <el-tabs v-model="activeIndex"
                 v-if="openTab.length"
                 type="card"
                 :closable = "tabCloseable"
                 @tab-click='tabClick'
                 @tab-remove="tabRemove"
10
                 style="width: 100%;margin-top: 0px;">
11 12 13 14 15 16
            <el-tab-pane
                    v-for="(item, index) in this.$store.state.openTab"
                    :key="item.name"
                    :label="item.name"
                    :name="item.route">
            </el-tab-pane>
朱兆平 authored
17 18 19 20
            <section class="content-container">
                <div>
                    <el-col :span="24" class="content-wrapper">
                        <transition name="fade" mode="out-in">
21 22 23
                            <keep-alive>
                                <router-view></router-view>
                            </keep-alive>
朱兆平 authored
24 25 26 27
                        </transition>
                    </el-col>
                </div>
            </section>
28 29 30 31 32 33
        </el-tabs>
    </div>
</template>

<script>
    import Assembly from "@/views/bus/Assembly";
小范 authored
34
    import Sortable from "sortablejs"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

    export default {
        name: 'TabMenu',
        components: { Assembly },

        data() {
            return {
                tabCloseable: true
            }
        },
        methods: {
            initTab(){
                // 刷新时以当前路由做为tab加入tabs
                // 当前路由不是首页时,添加首页以及另一页到store里,并设置激活状态
                // 当当前路由是首页时,添加首页到store,并设置激活状态
                if (this.$route.path !== '/' && this.$route.path !== '/main') {
朱兆平 authored
51
                    console.log('1');
52 53 54 55
                    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 {
朱兆平 authored
56
                    console.log('2');
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
                    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
87 88 89 90 91 92 93 94 95 96 97 98 99 100
            // changeView(val){
            //     if(val == 'table') {this.rowDrop()}
            //     this.initTab()
            // },
            rowDrop() {
                const el= document.querySelector('.el-tabs__nav');
                const _this = this;
                Sortable.create(el, {
                    onEnd({ newIndex, oldIndex }) {
                        const currRow = _this.$store.state.openTab.splice(oldIndex, 1)[0]
                        _this.$store.state.openTab.splice(newIndex, 0, currRow)
                    }
                })
            },
101 102 103
        },
        mounted() {
            this.initTab();
小范 authored
104 105 106 107
            this.$nextTick(function () {
                this.rowDrop();   //行拖拽效果
            })
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 151 152 153 154 155 156 157 158 159 160 161 162
        },
        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);
                }

            },
            openTab(){
                this.openTab.length === 1 ? this.tabCloseable=false :this.tabCloseable = true;
            }
        },
        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>
小范 authored
163 164 165 166
    .el-tabs__item{
        width: 13em;
        text-align: center;
    }
167 168
    /*底层背景色*/
    .el-tabs__nav-scroll{
169
        /*background-color: rgb(239,243,246);*/
170
    }
171 172 173 174
    .el-tabs--card>.el-tabs__header{
        border: none;
    }
        /*首个内部边框*/
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    .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);
191
        margin: 0px 3px;
192
        line-height: 26px;
193
        border-radius: 0 0 5px 5px;
194
        background-color: #fff;
小范 authored
195
        font-size: 12px;
196 197 198 199 200
        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);
201
        border-radius: 0 0 5px 5px;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        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;
    }
</style>