vue-admin-template模板添加 tagsview
从vue-element-admin复制文件:
- vue-admin-template\src\layout\components\TagsView 文件夹
- vue-admin-template\src\store\modules\tagsView.js
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></router-view>
</keep-alive>
</transition>
</section>
</template>
export default {
name: 'AppMain',
computed: {
cachedViews() {
return this.$store.state.tagsView.cachedViews
}/*,
key() {
return this.$route.path
}*/
}
}
<style lang="scss" scoped>
.app-main {
/*50 = navbar */
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>
修改vue-admin-template\src\layout\components\index.js,新增如下行:
export { default as TagsView } from './TagsView'
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="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,
permission,
settings,
tagsView,
user
},
getters
})
修改vue-admin-template\src\settings.js 添加
tagsView: true,
修改vue-admin-template\src\store\modules\settings.js
const { showSettings, tagsView, fixedHeader, sidebarLogo } = defaultSettings
const state = {
showSettings: showSettings,
tagsView: tagsView,
fixedHeader: fixedHeader,
sidebarLogo: sidebarLogo
}
解决控制台报错及常见需求
没有用到权限校验
判断是否为空,否则控制台会报错
循环引用报错
Error in callback for watcher "$route": "TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Vue'
| property '$options' -> object with constructor 'Object'
| property 'router' -> object with constructor 'VueRouter'
--- property 'app' closes the circle"
- 修改vue-admin-template\src\store\modules\tagsView.js
const mutations = {
ADD_VISITED_VIEW: (state, view) => {
if (state.visitedViews.some(v => v.meta.title === view.meta.title)) return;
let newView = {
fullPath: view.fullPath,
hash: view.hash,
meta: { ...view.meta },
name: view.name,
params: { ...view.params },
path: view.path,
query: { ...view.query },
title: view.title,
}
if (newView.meta.title) { // 没有名字的不添加标签
state.visitedViews.push(
Object.assign({}, newView, {
title: newView.meta.title
})
)
}
},
tabs-view 不可删除
- 加上 affix:true
{
path: 'dashboard',
name: 'dashboard',
component: () => import('@/views/dashboard/index'),
meta: {
title: '首页', icon: 'dashboard', affix: true
},
},
刷新后标签栏消失了
- 在vue-admin-template\src\layout\components\tagsView\index.vue添加
- mounted中执行
beforeUnload() {
// 监听页面刷新
window.addEventListener("beforeunload", () => {
// visitedViews数据结构太复杂无法直接JSON.stringify处理,先转换需要的数据
console.log(this.visitedViews,'this.visitedViews')
let tabViews = this.visitedViews.map((item) => {
return {
fullPath: item.fullPath,
hash: item.hash,
meta: { ...item.meta },
name: item.name,
params: { ...item.params },
path: item.path,
query: { ...item.query },
title: item.title,
};
});
sessionStorage.setItem("tabViews", JSON.stringify(tabViews));
});
// 页面初始化加载判断缓存中是否有数据
let oldViews = JSON.parse(sessionStorage.getItem("tabViews")) || [];
console.log(oldViews,'this.visitedViews2')
if (oldViews.length > 0) {
this.$store.state.tagsView.visitedViews = oldViews;
}
},
mounted() {
this.initTags();
this.addTags();
// 刷新页面标签不丢失
this.beforeUnload();
},
退出删除标签页
- 可以在路由全局守卫中判断是否跳转到登陆页,然后将vue-admin-template\src\store\modules\tagsView.js中的visitedViews置为
本文来自博客园,作者:懒惰ing,转载请注明原文链接:https://www.cnblogs.com/landuo629/p/14967401.html
分类:
Vue Cli 的使用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-07-03 uni-app 布尔提示框
2020-07-03 splice 与 slice