按照作者所說的,需要什么功能,從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\static 文件夾
#vue-admin-template\src\lang 文件夾
#vue-admin-template\src\utils\i18n.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 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 } }] },
以上步驟,親測可行。