包括后台项目 layout 基础架构、用户主动和被动退出、动态 menu 菜单、结构路由表、侧边菜单栏的收缩、组件状态驱动的动态 CSS 值
layout 基础架构布局
layout 基本布局结构包括:Menu 菜单栏、NavBar 顶部导航栏(包含个人信息)、Main(页面主要展示区域)
1、登录成功进入首页之后,整个首页也就是上面说的分为三个部分
创建三个对应的组件
layout/components/Sidebar/index.vue
layout/components/Navbar.vue
layout/components/AppMain.vue
2、在layout/index
引入创建的三个组件
1 |
|
3、layout/index
对应的布局
1 |
|
4、在 styles 文件夹下定义 3 个 scss 文件
variables.scss 定义常量
mixin.scss 定义通用的 css
sidebar.scss 处理 menu 菜单的样式
1 |
|
注意
:这里是使用 :export{ } 方法将 css 常量导出
mixin.scss
1 |
|
sidebar.scss
点我展示代码
1 |
|
5、创建的三个 scss 文件导入到 styles/index.scss
1 |
|
layout/index.vue
写入样式
1 |
|
6、侧边栏
要实现主题更换
功能,sidebar 赋值动态背景颜色
1 |
|
7、Navbar、Sidebar、AppMain 组件初始化代码占位及样式修改
需要处理 AppMain 组件和 Navbar 组件重叠问题
1 |
|
8、用户个人信息
这里需要处理一下用户个人信息展示
api/sys.js
文件定义方法、axios 请求拦截器对 token 进行 header 注入
1 |
|
定义调用接口的动作store/modules/user.js
1 |
|
在权限拦截时触发的动作permission.js
1 |
|
写入判断用户信息store/getters.js
1 |
|
9、element-plus 的 dropdown avatar 组件实现用户头像信息及用户主动退出功能
用户头像功能及下拉菜单栏
点我展示代码
1 |
|
10、主动退出功能
:在 store/moduels/user.js 中,清除当前用户缓存数据、清除用户权限相关配置、返回到登录页
1 |
|
退出按钮点击事件,触发 logout 的 action
1 |
|
11、被动退出功能
:token 过期、单一账户同时登录
token 表示一个用户的身份令牌,对服务端而言,只认令牌不认人,所以说一旦其他人获取了你的 token,那么可以伪装成你,来获取对应的敏感数据。
为了保证用户的信息安全,那么对于 token 而言就被制定了很多安全策略。
token 的安全策略:动态 token、刷新 token、失效 token
选择时效 token:服务端处理 token 时效的同时,在前端主动介入 token 时效的处理中,从而保证用户信息的安全性。
实现方案:
1、在用户登录时,记录当前登录时间
2、制定失效时长
3、接口调用时,根据当前时间对比登录时间,看是否超过了时效时长。如果未超过,则正常进行后续操作;如果超过,则进行退出登录操作
1 |
|
用户登录成功之后设置时间,到 store/user.js 的 login 中:
1 |
|
在utils/request
对应的请求拦截器中进行主动介入
1 |
|
12、临时menu菜单
layout/Sidebar/SidebarMenu.vue
1 |
|
layout/Sidebar/index 导入该组件
1 |
|
生成一个临时的 menu 菜单,从这个临时的 menu 菜单可以看出,el-menu 分为三部分:
el-menu
:整个 menu 菜单el-sub-menu
:子集 menu 菜单el-menu-item
:具体菜单项
13、动态 menu 菜单处理
动态 menu 菜单主要和动态路由表配合来实现用户权限的(后续需要完成用户权限)。动态 menu 菜单指的是:根据路由表的配置,自动生成对应的 menu 菜单,当路由表发生变化时,menu 菜单自动发生变化。
实现步骤:
1、定义路由表对应的 menu 菜单规则(核心步骤)
2、根据规则制定路由表
3、根据规则,一路路由表生成 menu 菜单
上面步骤 1 的菜单规则制定:
1、如果
meta && meta.title && meta.icon
:则显示在menu
菜单中,其中title
为显示的内容,icon
为显示的图标。如果存在 children:则展示子菜单 el-sub-menu;否则展示 el-menu-item。
2、不显示在 menu 中
14、创建页面组件
views 文件夹下创建页面:
(1)、创建文章:article-create
(2)、文章详情:article-detail
(3)、文章列表:article-ranking
(4)、401 和 404 页面:error-page/401.vue 404.vue
(5)、导入:import
(6)、权限列表:permission-list
(7)、个人中心:profile
(8)、角色列表:role-list
(9)、用户信息:user-info
(10)、用户管理:user-manage
15、用户权限处理
需要先对路由表进行划分:私有路由表 privateRoutes(权限路由)、公有路由表 publicRoutes(非权限路由)
router/index.js
显示详细代码
1 |
|
注意:要在
layout/AppMain.vue 中设置路由出口
1 |
|
16、解析路由表,获取结构化数据
数据结构为
显示详细代码
1 |
|
获取路由表数据的 2 中方式:
1、router.options.routes:初始路由列表
2、router.getRoutes():获取所有路由记录的完整列表(选择此种方法)
在 layout/components/Sidebar/SidebarMenu 中
1 |
|
打印的结果是一个完整的路由表,里面存在一些问题:
存在重复的路由数据
不满足该条件 meta && meta.title && meta.icon 的数据不应该存在
17、处理数据结构问题
创建utils/route.js
,里面 2 个函数方法:filterRouters()/generateMenus()很重要
显示完整代码
1 |
|
18、SidebarMenu 调用 filterRouters、generateMenus 方法
1 |
|
19、生成动态 menu 菜单
整个 menu 菜单,分为 3 个组件来进行处理
1、SidebarMenu:处理数据,作为最顶层 menu 载体
1 |
|
2、SidebarItem:根据数据处理当前项为 el-sub-menu || el-menu-item
1 |
|
3、MenuItem:处理 el-menu-item 样式
1 |
|
menu 菜单完成,还需解决样式问题、菜单路由跳转问题、默认激活项
样式修改
:store/getters 创建快捷访问(后续加入主题更换功能,样式不能写死)
1 |
|
SidebarMenu
写入样式(el-menu 里的 router 是指定路由跳转, activeMenu 为默认激活项)
1 |
|
20、菜单的伸缩功能
创建store/modules/app.js
1 |
|
在store/index.js
中引入上面的 app.js
1 |
|
在 store/getters
中创建快捷访问
1 |
|
21、创建收缩 icon 组件 hamburger
创建src/components/Hamburger/index.vue
1 |
|
在layout/components/Navbar.vue
中使用 Hamburger 组件
1 |
|
在layout/components/Sidebar/SidebarMenu.vue
中添加控制 el-menu 的collapse
属性
1 |
|
在layout/index.vue
中指定侧边栏的宽度和缩放动画
1 |
|
22、SidebarHeader 处理
显示详细代码
1 |
|
处理 el-avatar 的图片背景问题
创建 styles/element.scss
,然后将其导入到style/index.scss
1 |
|
处理动画问题
在 styles/variables.scss
中,加入以下变量
1 |
|
styles/sidebar.scss
修改时长
1 |
|
layout/index
修改样式
1 |
|
23、组件状态驱动的动态 CSS 值
1 |
|