vue-element-template是没有tagsview上面的那个tags的会显得比较呆板比较笨重没有很灵活,所以改了一下想更加灵活的去运用该模板,于是网上查询了很多资料,因此想记录下来自己以后的时候可以用!!!
一、从vue-element-admin复制文件到vue-element-admin的相同位置:
1、vue-admin-template\src\layout\components\TagsView 文件夹
2、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 />
</keep-alive>
</transition>
</section>
</template>
<script>
export default {
name: 'AppMain',
computed: {
key() {
return this.$route.path
},
// 新增
cachedViews() {
return this.$store.state.tagsView.cachedViews
}
}
}
</script>
<style scoped>
.app-main {
/*50 = navbar */
min-height: calc(100vh - 50px);
width: 100%;
position: relative;
overflow: hidden;
}
.fixed-header+.app-main {
padding-top: 50px;
}
</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 Navbar } from './Navbar'
export { default as Sidebar } from './Sidebar'
export { default as AppMain } from './AppMain'
// 新增的
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-->
<tags-view />
<app-main />
</div>
</div>
</template>
<script>
// 新增TagsView
import { Navbar, Sidebar, AppMain, TagsView } from './components'
import ResizeMixin from './mixin/ResizeHandler'
export default {
name: 'Layout',
components: {
Navbar,
Sidebar,
AppMain,
// 新增的
TagsView
},
mixins: [ResizeMixin],
computed: {
sidebar() {
return this.$store.state.app.sidebar
},
device() {
return this.$store.state.app.device
},
fixedHeader() {
return this.$store.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>
五、修改 vue-admin-template\src\store\getters.js
const getters = {
sidebar: state => state.app.sidebar,
device: state => state.app.device,
token: state => state.user.token,
avatar: state => state.user.avatar,
name: state => state.user.name,
menus: state => state.user.menus,
// 新增
visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews
}
export default getters
六、修改 vue-admin-template\src\store\index.js 修改到这里能显示出来了
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import tagsView from './modules/tagsView' // 这个是新增的
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
app,
settings,
user,
tagsView // 这个是新增的
},
getters
})
export default store
七、修改vue-admin-template\src\settings.js
module.exports = {
title: 'Vue Admin Template',
// 新增的
tagsView: true,
/**
* @type {boolean} true | false
* @description Whether fix the header
*/
fixedHeader: false,
/**
* @type {boolean} true | false
* @description Whether show the logo in sidebar
*/
sidebarLogo: false
}
八、修改vue-admin-template\src\store\modules\settings.js
import defaultSettings from '@/settings'
// 新增 tagsView
const { showSettings, fixedHeader, sidebarLogo, tagsView } = defaultSettings
const state = {
showSettings: showSettings,
// 新增 tagsView
tagsView: tagsView,
fixedHeader: fixedHeader,
sidebarLogo: sidebarLogo
}
const mutations = {
CHANGE_SETTING: (state, { key, value }) => {
// eslint-disable-next-line no-prototype-builtins
if (state.hasOwnProperty(key)) {
state[key] = value
}
}
}
const actions = {
changeSetting({ commit }, data) {
commit('CHANGE_SETTING', data)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
九、解决控制台报错:
1、删除vue-admin-template\src\layout\components\TagsView\index.vue中routes方法(因为没有用到权限校验) 加个if判断就行
visitedViews() {
return this.$store.state.tagsView.visitedViews
}
// 注释掉这个方法
// routes() {
// return this.$store.state.permission.routes
// }
},
methods: {
isActive(route) {
return route.path === this.$route.path
},
isAffix(tag) {
return tag.meta && tag.meta.affix
},
filterAffixTags(routes, basePath = '/') {
let tags = []
// 新增 IF判断
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
},
十、最后如果你要固定住tagsView,那么这里你需要设置下。路径:vue-admin-template\src\settings.js
module.exports = {
title: 'Vue Admin Template',
tagsView: true,
/**
* @type {boolean} true | false
* @description Whether fix the header
*/
fixedHeader: false, // 把fixedHEader: 改成ture就可以了
/**
* @type {boolean} true | false
* @description Whether show the logo in sidebar
*/
sidebarLogo: false
}