vue-element-template实战(三)增加多标签页

按照作者所说的,需要什么功能,从vue-element-admin-master中拷贝过来使用即可。

一、从vue-element-admin复制文件:

  vue-admin-template\src\layout\components\TagsView  文件夹
  vue-admin-template\src\store\modules\tagsView.js

二、修改 vue-admin-template\src\layout\components\AppMain.vue :AppMain.vue文件,修改如下:

<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <!-- <router-view :key="key" />-->
      <keep-alive :include="cachedViews">
        <router-view :key="key" />
      </keep-alive>
    </transition>
  </section>
</template>
<script>
export default {
  name: 'AppMain',
  computed: {
    //需要缓存的页面 固钉
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.fullPath
    }
  }
}
</script>
<style lang="scss" scoped>
.hasTagsView {
  .app-main {
    /* 84 = navbar + tags-view = 50 + 34 */
    min-height: calc(100vh - 84px);
  }

  .fixed-header+.app-main {
    padding-top: 84px;
  }
}
</style>

<style lang="scss">
// fix css style bug in open el-dialog
.el-popup-parent--hidden {
  .fixed-header {
    padding-right: 15px;
  }
}
</style>

三、修改vue-admin-template\src\layout\components\index.js,新增如下行

export { default as TagsView } from './TagsView'

四、在 vue-admin-template\src\layout\index.vue 文件中 新增 tagsview标签

<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <sidebar class="sidebar-container" />
    <div class="main-container">
      <div :class="{'fixed-header':fixedHeader}">
        <navbar />
      </div>
      <tags-view />    <!-- 此处增加tag-->
      <app-main />
    </div>
  </div>
</template>
import { Navbar, Sidebar, AppMain,TagsView } from './components'
components: {
    Navbar,
    Sidebar,
    AppMain,
    TagsView //新增
  },

五、vue-admin-template\src\store\getters.js,增加:

visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews,

六、修改 vue-admin-template\src\store\index.js

import tagsView from './modules/tagsView'
const store = new Vuex.Store({
  modules: {
    app,
    settings,
    tagsView, //此处新增
    user
  },
  getters
})

七、修改vue-admin-template\src\settings.js 添加

tagsView: true,

设置为根据值,是否选择加载。

八、修改vue-admin-template\src\store\modules\settings.js

const { showSettings, fixedHeader, sidebarLogo,tagsView } = defaultSettings
const state = {
  showSettings: showSettings,
  tagsView: tagsView, //此处新增
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo
}

 九、解决问题

1、删除vue-admin-template\src\layout\components\TagsView\index.vue中routes方法(因为没有用到权限校验)

computed: {
    visitedViews() {
      return this.$store.state.tagsView.visitedViews
    }/*,
    routes() {
      return this.$store.state.permission.routes
    }*/
  },

2、遍历标签时可能控制台报错

filterAffixTags(routes, basePath = '/') {
      let tags = []
      //判断是否为空,否则控制台会报错
      if(this.routes){
        routes.forEach(route => {
          if (route.meta && route.meta.affix) {
            const tagPath = path.resolve(basePath, route.path)
            tags.push({
              fullPath: tagPath,
              path: tagPath,
              name: route.name,
              meta: { ...route.meta }
            })
          }
          if (route.children) {
            const tempTags = this.filterAffixTags(route.children, route.path)
            if (tempTags.length >= 1) {
              tags = [...tags, ...tempTags]
            }
          }
        })
      }
      return tags
    },

3、删除全部标签后,会跳到404页面,所以要固定一些版面是不允许删除的。

在meta中加上affix: true就能使它不可删除,例如:meta: { title: '首页', icon: 'dashboard', affix: true }

{
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '后台管理系统', icon: 'dashboard' , affix: true  }
    }]
  },

 

以上步骤,亲测可行。

posted @ 2019-05-19 20:46  世界因你而小  阅读(4703)  评论(3编辑  收藏  举报