Commit 05d1d9dc by chuzhixin

🔥🔥🔥完善路由后端渲染方案,弃用之前写法

parent dc02bb01
......@@ -4,7 +4,7 @@
*/
import { asyncRoutes, constantRoutes } from "@/router";
import { getRouterList } from "@/api/router";
import { filterAllRoutes, filterAsyncRoutes } from "@/utils/handleRoutes";
import { convertRouter, filterAsyncRoutes } from "@/utils/handleRoutes";
const state = { routes: [], partialRoutes: [] };
const getters = {
......@@ -35,7 +35,7 @@ const actions = {
async setAllRoutes({ commit }) {
let { data } = await getRouterList();
data.push({ path: "*", redirect: "/404", hidden: true });
let accessRoutes = filterAllRoutes(data);
let accessRoutes = convertRouter(data);
commit("setAllRoutes", accessRoutes);
return accessRoutes;
},
......
......@@ -4,8 +4,8 @@
* @param constantRoutes
* @returns {*}
*/
export function filterAllRoutes(constantRoutes) {
return constantRoutes.filter((route) => {
export function convertRouter(asyncRoutes) {
return asyncRoutes.map((route) => {
if (route.component) {
if (route.component === "Layout") {
route.component = (resolve) => require(["@/layouts"], resolve);
......@@ -13,29 +13,16 @@ export function filterAllRoutes(constantRoutes) {
route.component = (resolve) =>
require(["@/layouts/EmptyLayout"], resolve);
} else {
let path = "views/" + route.component;
if (
new RegExp("^/views/.*$").test(route.component) ||
new RegExp("^views/.*$").test(route.component)
) {
path = route.component;
} else if (new RegExp("^/.*$").test(route.component)) {
path = "views" + route.component;
} else if (new RegExp("^@views/.*$").test(route.component)) {
path = route.component.slice(1);
} else {
path = "views/" + route.component;
}
const index = route.component.indexOf("views");
const path =
index > 0 ? route.component.slice(index) : `views/${route.component}`;
route.component = (resolve) => require([`@/${path}`], resolve);
}
}
if (route.children && route.children.length) {
route.children = filterAllRoutes(route.children);
}
if (route.children && route.children.length === 0) {
delete route.children;
}
return true;
if (route.children && route.children.length)
route.children = convertRouter(route.children);
if (route.children && route.children.length === 0) delete route.children;
return route;
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment