菜单

TagsView 与缓存

相关源文件

目的与范围

本文档将介绍 vue-element-admin 中的 TagsView 组件和路由缓存系统。TagsView 提供了一个基于选项卡(tab)的导航界面,用于显示和跟踪已访问的路由,而缓存机制则保留组件状态,以提高视图之间的导航性能。这两项功能共同增强了用户体验,使用户能够快速导航到常用页面,同时不丢失其状态。

1. TagsView 概览

TagsView 是一个导航组件,显示为主导航栏下方的标签行,代表用户已访问的路由。它充当视觉化的导航历史记录,并提供对最近使用页面的快速访问。

来源: src/layout/components/TagsView/index.vue1-26 src/store/modules/tagsView.js1-19

2. 架构与组件

TagsView 系统主要由两部分组成:

  1. 渲染标签的 UI 组件
  2. 管理已访问和已缓存视图状态的 Vuex 存储模块

来源: src/layout/components/TagsView/index.vue1-26 src/store/modules/tagsView.js1-4

2.1 Vuex 存储模块

tagsView Vuex 模块管理两个关键数组:

  1. visitedViews:包含用于渲染标签的完整路由对象
  2. 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

2.2 TagsView 组件

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

3. 缓存机制

缓存系统利用 Vue 内置的 <keep-alive> 组件,在导航路由之间保留组件状态。

3.1 视图缓存如何工作

来源: src/store/modules/tagsView.js15-20 src/components/SizeSelect/index.vue33-53

3.2 缓存控制

路由可以通过路由配置中的 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

4. 常用操作

TagsView 系统提供了几种用于管理标签和缓存视图的操作:

操作描述实现
添加标签访问路由时添加新标签addView action
关闭标签删除单个标签closeSelectedTag 方法
关闭其他只保留当前标签closeOthersTags 方法
关闭全部删除所有标签closeAllTags 方法
刷新视图重新渲染当前视图refreshSelectedTag 方法

来源: src/layout/components/TagsView/index.vue127-172 src/store/modules/tagsView.js69-152

4.1 刷新视图

刷新视图是一项特殊操作,它:

  1. cachedViews 中移除路由,以强制重新渲染
  2. 使用重定向路由返回同一页面

来源: src/layout/components/TagsView/index.vue127-136 src/components/SizeSelect/index.vue42-52

5. 固定标签

TagsView 支持“固定”标签,这些标签始终显示且无法关闭。它们通过在路由配置中设置 meta.affix: true 来定义。

来源: src/layout/components/TagsView/index.vue72-74 src/layout/components/TagsView/index.vue96-103

6. 与布局系统的集成

TagsView 组件位于主布局中,位于 Navbar 下方、主内容区域上方。

来源: src/layout/components/TagsView/index.vue1-26 src/layout/components/Navbar.vue1-9

7. 右键菜单功能

TagsView 提供了一个右键上下文菜单,用于对标签执行操作。

来源: src/layout/components/TagsView/index.vue19-24 src/layout/components/TagsView/index.vue173-195

8. 用例:切换尺寸

SizeSelect 组件展示了缓存系统如何与其他功能集成。在更改 UI 尺寸时,它会:

  1. 清除所有已缓存的视图,以确保组件以新尺寸重新渲染
  2. 使用重定向路由强制刷新当前视图

来源: src/components/SizeSelect/index.vue33-53

结论

vue-element-admin 中的 TagsView 和缓存系统提供了一种直观的导航体验,同时通过组件重用优化了性能。它展示了使用 Vuex 进行状态管理和 Vue Router 进行导航控制的结构化实现,从而带来了响应迅速且能保持状态的用户界面。