From 7732eee52d5615a386fe2fd97354ba18e5eac5e3 Mon Sep 17 00:00:00 2001 From: zhangqi <2794379662@qq.com> Date: Fri, 5 Feb 2021 11:11:25 +0800 Subject: [PATCH] =?UTF-8?q?router:=E6=94=B9=E9=80=A0=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=AE=BE=E7=BD=AE=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 91 ++++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 2c7247b..1687b30 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,45 +1,58 @@ import { createRouter, createWebHashHistory } from "vue-router"; -import Home from "../views/Home.vue"; -import CA from "../views/CA.vue"; -import CA_Index from "../views/CA/Index.vue"; -import CA_User from "../views/CA/UserApply.vue"; -import CA_Unit from "../views/CA/UnitApply.vue"; -import CA_Unit_composition from "../views/CA/UnitApply.composition.vue"; - -const routes = [ - { - path: "/", - name: "Home", - component: Home - }, - { - path: "/CA", - component: CA, - name: "数字证书申领", - children: [ - { - path: "", - component: CA_Index - }, - { - path: "user", - component: CA_User, - name: "个人数字证书申领" - }, - { - path: "unit", - component: CA_Unit, - name: "企业数字证书申领" - }, - { - path: "composition", - component: CA_Unit_composition, - name: "企业数字证书申领新" - } - ] +const files = require.context("@/views", true, /\.vue$/); +let pages = []; +const genRoutes = []; +const pathName = (route, idx) => { + if (idx == undefined) idx = route.length - 1; + let path = route[idx]; + path = `${idx == 0 ? "/" : ""}${path}`; + if (path == "/Home") { + path = "/"; } -]; + if (path == "Index") { + path = ""; + } + return path; +}; +const findRoute = routeArray => { + let routes = genRoutes; + let route = null; + for (let i = 0; i < routeArray.length - 1; i++) { + route = routes.find(p => p.path == pathName(routeArray, i)); + if (route) routes = route.children || []; + } + return route; +}; +files.keys().forEach(key => { + const r = key + .replace("./", "") + .replace(".vue", "") + .split("/") + .filter(k => k !== ""); + pages.push({ route: r, component: files(key).default }); +}); +pages + .sort((a, b) => a.route.length - b.route.length) + .forEach(e => { + const route = findRoute(e.route); + let path = pathName(e.route); + if (!route) { + genRoutes.push({ + path, + name: e.component.name, + component: e.component + }); + } else { + route.children = route.children || []; + route.children.push({ + path, + name: e.component.name, + component: e.component + }); + } + }); +const routes = [...genRoutes]; const router = createRouter({ history: createWebHashHistory(), routes