一、复制文件:
从vue-element-admin 中复制到项目中去
vue-admin-template\src\layout\components\TagsView 文件夹
vue-admin-template\src\store\modules\tagsView.js
二、文件修改
1. vue-admin-template\src\layout\components\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>
2.vue-admin-template\src\layout\components\index.js
新增如下行
export { default as TagsView } from './TagsView'
3. 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 //新增
},
4. vue-admin-template\src\store\getters.js
增加如下
visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews,
5. vue-admin-template\src\store\index.js
修改如下
import tagsView from './modules/tagsView'
const store = new Vuex.Store({
modules: {
app,
settings,
tagsView, //此处新增
user
},
getters
})
6.vue-admin-template\src\settings.js
添加配置如下
tagsView: true,
设置为根据值,是否选择加载。
7.vue-admin-template\src\store\modules\settings.js
修改如下
const { showSettings, fixedHeader, sidebarLogo,tagsView } = defaultSettings
const state = {
showSettings: showSettings,
tagsView: tagsView, //此处新增
fixedHeader: fixedHeader,
sidebarLogo: sidebarLogo
}
三、解决问题
- 删除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
}*/
},
- 遍历标签时可能控制台报错
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
},
- 删除全部标签后,会跳到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 }
}]
},
- 如果右击标签刷新跳转404页面, 记得在vue-admin-template\src\views 目录下将redirecy复制进去