基于 vue-element-template 框架添加 tagsview

1. 需求

vue-element-template 是一个基础模板,默认没有 tagsview。所以要手动添加。
参考最全面的集成方案框架 vue-element-admin ,拷贝和修改相关文件到你的项目中。

2. 修改

  1. 复制如下文件或文件夹
\src\layout\components\TagsView
 
\src\store\modules\tagsView.js
  1. 修改文件 \src\layout\components\AppMain.vue。
<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <!-- <router-view :key="key" /> -->
      <!-- 使用 keep-alive 包裹 -->
      <keep-alive :include="cachedViews">
        <router-view :key="key" />
      </keep-alive>
    </transition>
  </section>
</template>

添加计算属性 cachedViews。

computed: {
  cachedViews() {
    return this.$store.state.tagsView.cachedViews
  },
  key() {
    return this.$route.path
  }
}

修改 css,如下代码直接拷贝覆盖。

<style lang="scss" scoped>
.app-main {
  /*50 = navbar 34px tagsview */
  min-height: calc(100vh - 84px);
  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>
  1. 修改文件 \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="main-container">
      <div :class="{'fixed-header':fixedHeader}">
        <navbar />
        <!-- 新增 -->
        <tags-view/>
      </div>
      <app-main />
    </div>
  </div>
</template>

添加 TagsView 组件的引用。

import { Navbar, Sidebar, AppMain, TagsView } from './components'
components: {
  Navbar,
  Sidebar,
  AppMain,
  TagsView
},
  1. 修改文件 \src\layout\components\index.js,导出 TagsView。
export { default as TagsView } from './TagsView/index.vue'
  1. 修改文件 \src\store\getters.js,添加属性 visitedViews 和 cachedViews。
visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews
  1. 修改文件 \src\store\index.js,导出 tagsview 相关信息。
import tagsView from './modules/tagsView'
const store = new Vuex.Store({
  modules: {
    app,
    settings,
    tagsView,// 新增
    user
  },
  getters
})
  1. 修改文件/src/store/modules/settings.js
const { showSettings, fixedHeader, sidebarLogo, tagsView } = defaultSettings

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  tagsView: tagsView
}
  1. 修改文件 \src\layout\components\TagsView\index.vue,屏蔽计算属性routes,因为暂时没用上权限。
computed: {
  visitedViews() {
    return this.$store.state.tagsView.visitedViews
  },
  // routes() {// 屏蔽权限,否则报错
  //   return this.$store.state.permission.routes
  // }
},

首次加载没有 tags-view,因为方法 filterAffixTags 报错 routes。修改方法 initTags。

initTags() {
  if(this.routes) {
    const affixTags = this.affixTags = this.filterAffixTags(this.routes)
    for (const tag of affixTags) {
      // Must have tag name
      if (tag.name) {
        this.$store.dispatch('tagsView/addVisitedView', tag)
      }
    }
  }
},
  1. 修改src/layout/index.vue,通过needTagsView判断
<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>

<script>
import { AppMain, Navbar, Sidebar, TagsView } from './components'
import ResizeMixin from './mixin/ResizeHandler'
import { mapState } from 'vuex'

export default {
  name: 'Layout',
  components: {
    AppMain,
    Navbar,
    Sidebar,
    TagsView
  },
  mixins: [ResizeMixin],
  computed: {
    ...mapState({
      sidebar: state => state.app.sidebar,
      device: state => state.app.device,
      showSettings: state => state.settings.showSettings,
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader
    }),
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    }
  },
  methods: {
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "~@/styles/mixin.scss";
  @import "~@/styles/variables.scss";

  .app-wrapper {
    @include clearfix;
    position: relative;
    height: 100%;
    width: 100%;

    &.mobile.openSidebar {
      position: fixed;
      top: 0;
    }
  }

  .drawer-bg {
    background: #000;
    opacity: 0.3;
    width: 100%;
    top: 0;
    height: 100%;
    position: absolute;
    z-index: 999;
  }

  .fixed-header {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9;
    width: calc(100% - #{$sideBarWidth});
    transition: width 0.28s;
  }

  .hideSidebar .fixed-header {
    width: calc(100% - 54px)
  }

  .mobile .fixed-header {
    width: 100%;
  }
</style>

效果图:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heromps

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值