我们已将完成了骨架大部分功能的实现,现在只需要一点小操作就可以让项目跑起来。
- 首先在我们需要登陆获取Token,
- 在路由守卫中如果有Token就会去请求用户信息,获取该用户的权限列表,
- 然后和路由中配置的权限进行匹配,剔除该用户没有权限访问的路由,
- 之后动态添加过滤之后的路由到总的路由中,此时用户就能重定向到可以访问的路由。
- 进入系统前,回根据路由信息动态生成菜单
通过上面步骤我们可知,必须要有Token,需要有用户信息且包含权限列表。
更改代码
使用token2绕过Token检查,注释掉获取用户信息步骤,自定义用户信息。
修改src/router/router-guards.js
,内容如下:
router/router-guards.jsimport { PageEnum } from '@/enums/pageEnum'; import { useAsyncRouteStoreWidthOut } from '@/store/modules/asyncRoute';
import { ACCESS_TOKEN } from '@/store/mutation-types'; import { storage } from '@/utils/Storage'; import { isNavigationFailure } from 'vue-router';
const LOGIN_PATH = PageEnum.BASE_LOGIN;
const whitePathList = [LOGIN_PATH];
export function createRouterGuards(router) { const asyncRouteStore = useAsyncRouteStoreWidthOut(); router.beforeEach(async (to, from, next) => { const Loading = window['$loading'] || null; Loading && Loading.start(); if (from.path === LOGIN_PATH && to.name === 'errorPage') { next(PageEnum.BASE_HOME); return; }
if (whitePathList.includes(to.path)) { next(); return; } const token = storage.get(ACCESS_TOKEN); const token2 = 'token' + token; if (!token2) { if (to.meta.ignoreAuth) { next(); return; } const redirectData = { path: LOGIN_PATH, replace: true, }; if (to.path) { redirectData.query = { ...redirectData.query, redirect: to.path, }; } next(redirectData); return; } if (asyncRouteStore.getIsDynamicAddedRoute) { next(); return; } const userInfo = { userId: '1', username: 'admin', realName: 'Admin', desc: 'manager', token, permissions: [ { label: '主控台', value: 'dashboard_console', }, { label: '工作台', value: 'dashboard_workplace', }, ], }; const routes = asyncRouteStore.generateRoutes(userInfo); routes.forEach((item) => { router.addRoute(item); });
const redirectPath = from.query.redirect || to.path; const redirect = decodeURIComponent(redirectPath); const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }; asyncRouteStore.setDynamicAddedRoute(true); next(nextData); Loading && Loading.finish(); });
router.afterEach((to, _, failure) => { document.title = to?.meta?.title || document.title; if (isNavigationFailure(failure)) { } const asyncRouteStore = useAsyncRouteStoreWidthOut(); const keepAliveComponents = asyncRouteStore.keepAliveComponents; const currentComName = to.matched.find( (item) => item.name == to.name )?.name; if ( currentComName && !keepAliveComponents.includes(currentComName) && to.meta?.keepAlive ) { keepAliveComponents.push(currentComName); } else if (!to.meta?.keepAlive || to.name == 'Redirect') { const index = asyncRouteStore.keepAliveComponents.findIndex( (name) => name == currentComName ); if (index != -1) { keepAliveComponents.splice(index, 1); } } asyncRouteStore.setKeepAliveComponents(keepAliveComponents); const Loading = window['$loading'] || null; Loading && Loading.finish(); });
router.onError((error) => { console.log(error, '路由错误'); }); }
|
格式问题
修改.eslintrc.js
,代码检查工具eslint的配置信息,
.eslintrc.jsmodule.exports = { root: true, env: { node: true, }, extends: [ "plugin:vue/vue3-essential", "eslint:recommended", "plugin:prettier/recommended", ], parserOptions: { parser: "@babel/eslint-parser", }, rules: { "no-console": process.env.NODE_ENV === "production" ? "warn" : "off", "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off", "vue/multi-word-component-names": "off", 'vue/no-mutating-props': 0, 'linebreak-style': [ 0,'error','windows',], 'quotes': [0, "single"], 'prettier/prettier': [ 'warn',{ singleQuote: true } ] }, };
|
添加修复命令
修改package.json
文件,添加lint-fix
,如下:
{ "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "lint-fix": "eslint --fix --ext .js --ext .jsx --ext .vue src/" }, }
|
在终端使用下面命令修复代码报错
启动项目
终端运行下面命令启动