做网站图片素材,郴州专业seo,备案中又需要建设网站,加大网站集约化建设管理1.动态获取权限并根据获取权限转换成相对应的router
根据请求获取菜单数据#xff0c;对菜单数据进行转换#xff0c;分别进行下面几步#xff1a;
/*** 组件地址前加斜杠处理*/
export function addSlashToRouteComponent(routeList: AppRouteRecordRaw[]) {routeList.fo…1.动态获取权限并根据获取权限转换成相对应的router
根据请求获取菜单数据对菜单数据进行转换分别进行下面几步
/*** 组件地址前加斜杠处理*/
export function addSlashToRouteComponent(routeList: AppRouteRecordRaw[]) {routeList.forEach((route) {const component route.component as string;if (component) {const layoutFound LayoutMap.get(component);if (!layoutFound) {route.component component.startsWith(/) ? component : /${component};}}route.children addSlashToRouteComponent(route.children);});return routeList;
}
利用import函数通配符引入匹配的所有vue页面,如下
const LayoutMap new Mapstring, () Promisetypeof import(*.vue)();
以及const dynamicViewsModules import.meta.glob(../../views/**/*.{vue,tsx});
该map会生成一个以页面路径为key的带有import动态引入的方法为值的对象这是可以通过处理key字符串通过获取的菜单列表匹配对应的path最终的component 就等于dynamicViewsModules[path]
function dynamicImport(dynamicViewsModules: Recordstring, () PromiseRecordable, component: string) {const keys Object.keys(dynamicViewsModules);const matchKeys keys.filter((key) {const k key.replace(../../views, );const startFlag component.startsWith(/);const endFlag component.endsWith(.vue) || component.endsWith(.tsx);const startIndex startFlag ? 0 : 1;const lastIndex endFlag ? k.length : k.lastIndexOf(.);return k.substring(startIndex, lastIndex) component;});if (matchKeys?.length 1) {const matchKey matchKeys[0];return dynamicViewsModules[matchKey];} else if (matchKeys?.length 1) {console.log(Please do not create .vue and .TSX files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure);return;}
}
处理后最终返回的数据就是生成router树状结构
2.动态添加路由addRoute
const menuRecordRoutes: RouteRecordRaw[] Object.entries(menuRoutes).map(([, config]) config).sort((a: any, b: any) a.order - b.order) as RouteRecordRaw[];
menuRecordRoutes.forEach((route) {router.addRoute(route);});
这里有个值得注意的地方如果是该菜单列表下有多个子菜单需要将子菜单也添加进去如
menuRecordRoutes.forEach((route) {router.addRoute(route);if (route?.children?.length) {route.children.forEach((child) {child.name route.name?.toString() - child.name?.toString();if (route.name) {router.addRoute(route.name, child);}});}});
上图中后面一个router.addRoute是针对每个子菜单进行router添加并且这里还有一个坑
如果不同的菜单下的子菜单的name属性是一样的话 这里可能会把前面一个添加的子菜单给覆盖了因为router中的name 属性是唯一的所以这里对子菜单的name做了一个拼接将父菜单名称和子菜单名称拼接到一起作为子菜单的新名称。另外这个name属性非常重要如果name属性中含有特殊符号如namedetailid页面中如果使用了类似这样的页面跳转router.push({ name: detail, params: { id: row.id } }); 则会导致路由不匹配的问题
3.自定义指令控制按钮显隐
//main.ts中引用
const app createApp(App);
// 全局注册 自定义指令(directive)
setupDirective(app);
import type { App } from vue;import { hasPerm } from ./permission;// 全局注册 directive
export function setupDirective(app: AppElement) {// 使 v-hasPerm 在所有组件中都可用app.directive(hasPerm, hasPerm);
}//自定义指令的自定义方法
export const hasPerm: Directive {mounted(el: HTMLElement, binding: DirectiveBinding) {// 「超级管理员」拥有所有的按钮权限const { authList } useUserStoreHook();const { value } binding;let result false;if (authList.length value) {if (Array.isArray(value)) {result value.every((ele) authList.includes(ele));} else {result authList.includes(value);}}if (!result) {el.parentNode el.parentNode.removeChild(el);}return result;},
};
页面中使用
el-button v-hasPerm[xxx] :iconDelete sizesmall text typeprimary删 除/el-button
至此动态菜单和按钮权限功能完成
文章转载自: http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.ntffl.cn.gov.cn.ntffl.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.kgjyy.cn.gov.cn.kgjyy.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn