搭建Vue从Vue-cli到router路由护卫的实现

这篇文章主要介绍了搭建Vue从Vue-cli到router路由护卫的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

别的不多说,开始动爪把,

首先安装vue-cli  mac: sudo npm install -g @vue/cli

github:

https://github.com/XinYueXiao/vue-routes

1、Vue-cli基础使用

1.1 创建测试项目 vue create vue-routes

1.2 创建成功,启动项目 yarn serve

在 http://localhost:8080/ 就可以看到欢迎:clap:页面了

1.3 搞点自定义配置,新建vue.config.js

 const title = '双11剁手啦' const port = '1111' module.exports = { publicPath: '/wxy', //自定义端口号 devServer: { port }, //自定义变量 configureWebpack: { name: title } }

配置完成后重新启动 yarn serve 效果图

如何配置svg图标

1)准备一个svg,例如: src/icons/svg/hg.svg

2)安装loader yarn add svg-sprite-loader

3)对config进行链式操作即可修改loader

 const path = require('path') //处理地址 function resolve(dir) { return path.join(__dirname, dir) } module.exports = { ..., chainWebpack(config) { //安装loader,对config进行链式操作即可修改loader、plugins //1.svg rule中要排除icons目录 config.module.rule('svg') //转换为绝对地址 .exclude.add(resolve('src/icons')) //查看配置后svg规则 vue inspect --rule svg //2.添加一个规则icons config.module.rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')).end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) } }

4)svg rule中要排除icons目录后配置

5) 添加一个规则icons配置

6) 新建 src/components/SvgIcon.vue 模板

 

7)新建 src/icons/index.js  在main.js下引入icon

 //src/icons/index.js import Vue from 'vue' import SvgIcon from '@/components/SvgIcon' //图标自动加载 const req = require.context('./svg', false, /\.svg$/) req.keys().map(req) Vue.component('svg-icon', SvgIcon) //main.js import "./icons";

8)在App.vue引入图标

 

效果如下:

2、router路由守卫

何为守卫,即为阻止无身份者进入组织内部

安装yarn add vue-router 控制路由

安装yarn add vuex 存储身份认证

2.1 路由配置

src/router/index.js

 import Vue from "vue"; import Router from "vue-router"; import Layout from '@/layout'; // 布局页 Vue.use(Router); // 通用页面:不需要守卫,可直接访问 export const constRoutes = [ { path: "/login", component: () => import("@/views/Login"), hidden: true // 导航菜单忽略该项 }, { path: "/", component: Layout,// 应用布局 redirect: "/home", children: [ { path: "home", component: () => import(/* webpackChunkName: "home" */ "@/views/Home.vue"), name: "home", meta: { title: "Home", // 导航菜单项标题 icon: "hg" // 导航菜单项图标 } }] }]; // 权限页面:受保护页面,要求用户登录并拥有访问权限的角色才能访问 export const asyncRoutes = [ { path: "/about", component: Layout, redirect: "/about/index", children: [ { path: "index", component: () => import(/* webpackChunkName: "home" */ "@/views/About.vue"), name: "about", meta: { title: "About", icon: "hg", roles: ['admin', 'editor'] }, } ] } ]; export default new Router({ mode: "history", base: process.env.BASE_URL, routes: constRoutes });

布局组件 src/layout

 

路由展示src/App.vue

 

2.2  准备页面

src/views/About.vue

 

src/views/Home.vue

 

src/views/Login.vue

 

2.3  身份认证

 import router from "./router"; import store from "./store"; const whiteList = ["/home", "/login"]; // 无需令牌白名单 // 全局路由守卫 router.beforeEach(async (to, from, next) => { // 获取令牌判断用户是否登录 const hasToken = localStorage.getItem("token"); // 已登录 if (hasToken) { if (to.path === "/login") { // 若已登录没有必要显示登录页,重定向至首页 next({ path: "/" }); } else { // 去其他路由,暂时放过 //  next() // 接下来执行用户角色逻辑, todo //  1.判断用户是否拥有角色 const hasRoles = store.state.user.roles && store.state.user.roles.length > 0; if (hasRoles) { next(); } else { // 2.获取用户角色 const roles = await store.dispatch("user/getInfo"); const accessRoutes = await store.dispatch("permission/generateRoutes", roles); //  动态添加路由到路由器 router.addRoutes(accessRoutes); // 跳转 next({ ...to }); } } } else { // 未登录 if (whiteList.indexOf(to.path) !== -1) { // 白名单中路由放过 next(); } else { // 重定向至登录页 next(`/login?redirect=${to.path}`); } } });

2.4  用户信息设置

 import Vue from "vue"; import Vuex from "vuex"; import user from './modules/user' import permission from './modules/permission' Vue.use(Vuex); export default new Vuex.Store({ modules: { user, permission } });

src/store/modules/user.js

 const state = { token: localStorage.getItem("token"), // 其他用户信息 roles: [] }; const mutations = { SET_TOKEN: (state, token) => { state.token = token; }, SET_ROLES: (state, roles) => { state.roles = roles; }, }; const actions = { // 模拟用户登录 login({ commit }, userInfo) { const { username } = userInfo; return new Promise((resolve, reject) => { setTimeout(() => { if (username === "admin" || username === "jerry") { commit("SET_TOKEN", username); localStorage.setItem("token", username); resolve(); } else { reject("用户名、密码错误"); } }, 1000); }); }, getInfo({ commit, state }) { return new Promise((resolve) => { setTimeout(() => { const roles = state.token === 'admin' ? ['admin'] : ['editor'] commit('SET_ROLES', roles) resolve(roles) }, 1000); }) } }; export default { namespaced: true, state, mutations, actions, };

2.5  用户路由权限 src/store/modules/permission.js

 // 导入asyncRoutes,过滤它看当前用户是否拥有响应权限 import {asyncRoutes, constRoutes} from '@/router' const state = { routes: [], // 完整路由 addRoutes: [], // 权限路由 } const mutations = { // routes: 用户可访问的权限路由 SET_ROUTES: (state, routes) => { state.addRoutes = routes; state.routes = constRoutes.concat(routes); } } const actions = { generateRoutes({commit}, roles) { // 过滤出能访问的路由表 const routes = filterAsyncRoutes(asyncRoutes, roles) commit('SET_ROUTES', routes) return routes; } } function filterAsyncRoutes(routes, roles) { const res = []; routes.forEach(route => { // 复制一份路由 const tmp = {...route}; // 拥有访问权限 if (hasPermission(roles, tmp)) { if (tmp.children) { // 递归子路由 tmp.children = filterAsyncRoutes(tmp.children, roles) } res.push(tmp); } }) return res; } function hasPermission(roles, route) { if (route.meta && route.meta.roles) { return roles.some(role => route.meta.roles.includes(role)) } else { // 路由定义中没有roles选项,则不需要权限即可访问 return true; } } export default { namespaced: true, state, mutations, actions }

2.6 最终效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是搭建Vue从Vue-cli到router路由护卫的实现的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Vue.js 教程