本文档将介绍 vue-element-admin 中的 TagsView 组件和路由缓存系统。TagsView 提供了一个基于选项卡(tab)的导航界面,用于显示和跟踪已访问的路由,而缓存机制则保留组件状态,以提高视图之间的导航性能。这两项功能共同增强了用户体验,使用户能够快速导航到常用页面,同时不丢失其状态。
TagsView 是一个导航组件,显示为主导航栏下方的标签行,代表用户已访问的路由。它充当视觉化的导航历史记录,并提供对最近使用页面的快速访问。
来源: src/layout/components/TagsView/index.vue1-26 src/store/modules/tagsView.js1-19
TagsView 系统主要由两部分组成:
来源: src/layout/components/TagsView/index.vue1-26 src/store/modules/tagsView.js1-4
tagsView Vuex 模块管理两个关键数组:
visitedViews:包含用于渲染标签的完整路由对象cachedViews:仅包含路由名称,用于使用 <keep-alive> 进行组件缓存State:
- visitedViews: Array of route objects (for UI display)
- cachedViews: Array of route names (for <keep-alive> component)
Mutations:
- ADD_VISITED_VIEW: Add a route to visitedViews
- ADD_CACHED_VIEW: Add a route name to cachedViews
- DEL_VISITED_VIEW: Remove a route from visitedViews
- DEL_CACHED_VIEW: Remove a route name from cachedViews
- DEL_OTHERS_VISITED_VIEWS: Remove all routes except current and affixed
- DEL_OTHERS_CACHED_VIEWS: Remove all cached routes except current
- DEL_ALL_VISITED_VIEWS: Remove all routes except affixed
- DEL_ALL_CACHED_VIEWS: Clear all cached routes
- UPDATE_VISITED_VIEW: Update a visited view
来源: src/store/modules/tagsView.js1-67
TagsView 组件根据 Vuex 存储中的 visitedViews 数组渲染标签,并提供交互功能。
Main Features:
- Displays tabs for each visited route
- Shows active state for current route
- Provides close button for each tag (except affixed)
- Middle-click to close a tag
- Right-click context menu with operations
- Horizontal scrolling for many tags
来源: src/layout/components/TagsView/index.vue1-26 src/layout/components/TagsView/index.vue33-68
缓存系统利用 Vue 内置的 <keep-alive> 组件,在导航路由之间保留组件状态。
来源: src/store/modules/tagsView.js15-20 src/components/SizeSelect/index.vue33-53
路由可以通过路由配置中的 meta.noCache 属性来控制其缓存行为。
Route with caching (default):
{
path: '/example',
name: 'Example',
component: Example,
meta: { title: 'Example' }
}
Route without caching:
{
path: '/example',
name: 'Example',
component: Example,
meta: {
title: 'Example',
noCache: true
}
}
来源: src/store/modules/tagsView.js15-19
TagsView 系统提供了几种用于管理标签和缓存视图的操作:
| 操作 | 描述 | 实现 |
|---|---|---|
| 添加标签 | 访问路由时添加新标签 | addView action |
| 关闭标签 | 删除单个标签 | closeSelectedTag 方法 |
| 关闭其他 | 只保留当前标签 | closeOthersTags 方法 |
| 关闭全部 | 删除所有标签 | closeAllTags 方法 |
| 刷新视图 | 重新渲染当前视图 | refreshSelectedTag 方法 |
来源: src/layout/components/TagsView/index.vue127-172 src/store/modules/tagsView.js69-152
刷新视图是一项特殊操作,它:
cachedViews 中移除路由,以强制重新渲染来源: src/layout/components/TagsView/index.vue127-136 src/components/SizeSelect/index.vue42-52
TagsView 支持“固定”标签,这些标签始终显示且无法关闭。它们通过在路由配置中设置 meta.affix: true 来定义。
来源: src/layout/components/TagsView/index.vue72-74 src/layout/components/TagsView/index.vue96-103
TagsView 组件位于主布局中,位于 Navbar 下方、主内容区域上方。
来源: src/layout/components/TagsView/index.vue1-26 src/layout/components/Navbar.vue1-9
TagsView 提供了一个右键上下文菜单,用于对标签执行操作。
来源: src/layout/components/TagsView/index.vue19-24 src/layout/components/TagsView/index.vue173-195
SizeSelect 组件展示了缓存系统如何与其他功能集成。在更改 UI 尺寸时,它会:
来源: src/components/SizeSelect/index.vue33-53
vue-element-admin 中的 TagsView 和缓存系统提供了一种直观的导航体验,同时通过组件重用优化了性能。它展示了使用 Vuex 进行状态管理和 Vue Router 进行导航控制的结构化实现,从而带来了响应迅速且能保持状态的用户界面。