Vue - vue-admin-template模板项目改造:增加TagsView功能

GitHub Demo 地址

在线预览

更新:对应的vue3版的demo如下:

GitHub Demo 地址

在线预览

vue-admin-template模板项目4.1版本默认没有TagsView功能,需要从vue-element-admin复制粘贴TagsView相关代码
相关步骤参考:https://github.com/PanJiaChen/vue-admin-template/issues/434

TagsView效果

在这里插入图片描述

准备工作:

下载vue-admin-template
下载vue-element-admin

操作步骤

1、从vue-element-admin复制文件到对应位置

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

2、替换vue-admin-template\src\layout\components\AppMain.vue

复制以下代码直接替换AppMain.vue

<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <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>
.app-main {
  /* 50= navbar  50  */
  min-height: calc(100vh - 50px);
  width: 100%;
  position: relative;
  overflow: hidden;
}

.fixed-header+.app-main {
  padding-top: 50px;
}

.hasTagsView {
  .app-main {
    /* 84 = navbar + tags-view = 50 + 34 */
    min-height: calc(100vh - 84px);
  }

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

3、修改vue-admin-template\src\layout\components\index.js

新增如下行

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

4、修改vue-admin-template\src\layout\index.vue

<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="{hasTagsView:needTagsView}" class="main-container">
      <div :class="{'fixed-header':fixedHeader}">
        <navbar />
        <tags-view v-if="needTagsView"/>
      </div>
      <app-main />
    </div>
  </div>
</template>
import { Navbar, Sidebar, AppMain, TagsView } from './components'
  components: {
    Navbar,
    Sidebar,
    AppMain,
    TagsView // 新增tagsView
  },

computed 方法中新增下面代码

 needTagsView() {
   return this.$store.state.settings.tagsView
 },

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

  // 新增tagsView
  visitedViews: state => state.tagsView.visitedViews,
  cachedViews: state => state.tagsView.cachedViews

6、vue-admin-template\src\store\index.js,增加:

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

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

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

8、修改vue-admin-template\src\router\index.js

路由表新增

  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },

9、修改vue-admin-template\src\settings.js

  fixedHeader: true,
  sidebarLogo: true,
  tagsView: true // tagsView显隐控制

10、解决控制台报错(因为没有用到权限校验)

修改:vue-admin-template\src\layout\components\TagsView\index.vue

注释permission.routes

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

routes添加判空

  if (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]
        }
      }
    })
  }

至此结束

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值