路由包括动态路由、静态路由两种,本文中以静态路由的方式实现了动态路由。

1. 创建Router

在Src目录下创建router文件夹,并在router文件夹下创建index.ts文件。

index.ts文件内容如下:

import { reactive } from "vue";
import * as VueRouter from "vue-router";
import { useUserStore } from "@/store/modules/user";
import { ElNotification } from "element-plus";
import dynamicRouter from "./dynamic";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false }); //系统特殊路由

const routes_404 = {

path: "/:pathMatch(.)",

hidden: true,

component: () => import("@/layout/other/404.vue"),

};

let routes_404_r = () => {};

const routes = [

{

name: "layout",

path: "/",

redirect: "/dashboard",

component: () => import("@/layout/index.vue"),

children: [],

},

{

path: "/login",

component: () => import("../views/login/index.vue"),

},

];

const router = VueRouter.createRouter({

history: VueRouter.createWebHistory(),

routes,

// 刷新时,滚动条位置还原

scrollBehavior: () => ({ left: 0, top: 0 }),

});

var isLoadRouter = false;

router.beforeEach(async (to, _from, next) => {

const userStore = useUserStore();

NProgress.start();

if (to.path === "/login" && !userStore.user) {

//删除路由(替换当前layout路由)

router.addRoute(routes[0]);

//删除路由(404)

routes_404_r();

isLoadRouter = false;

next();

return false;

}

if (to.path === "/login" && userStore.user) {

next("/");

return false;

}

if (!userStore.user) {

next("/login");

return false;

}

//整页路由处理

if (to.meta.fullpage) {

to.matched = [to.matched[to.matched.length - 1]];

}

//加载动态路由 dynamicRouter 如果通过程序加载,可以通过localStorage在登录页面存入路由数据,并在这里localStorage取出来

if (!isLoadRouter) {

var menuRouter = filterAsyncRouter(dynamicRouter);

menuRouter = flatAsyncRoutes(menuRouter);

menuRouter.forEach((item) => {

router.addRoute("layout", item);

});

routes_404_r = router.addRoute(routes_404);

if (to.matched.length == 0) {

router.push(to.fullPath);

}

isLoadRouter = true;

}

next();

NProgress.done();

});

router.afterEach(() => {

NProgress.done();

});

router.onError((error) => {

NProgress.done();

ElNotification.error({

title: "路由错误",

message: error.message,

});

});

function filterAsyncRouter(routerMap) {

const accessedRouters: any[] = [];

routerMap.forEach((item) => {

item.meta = item.meta ? item.meta : {};

//处理外部链接特殊路由

if (item.meta.type == "iframe") {

item.meta.url = item.path;

item.path = /i/${item.name};

}

//MAP转路由对象

var route = {

path: item.path,

name: item.name,

meta: item.meta,

redirect: item.redirect,

children: item.children ? filterAsyncRouter(item.children) : null,

component: resolveComponent(item.component),

};

accessedRouters.push(route);

});

return accessedRouters;

}

// 加载.vue文件

const pages = import.meta.glob("@/views/**/*.vue");

const resolveComponent = (name: any) => {

const importPage = pages[/src/views/${name}.vue];

if (!importPage) {

return () => import(@/layout/other/empty.vue);

}

return importPage;

};

function flatAsyncRoutes(routes, breadcrumb = []) {

let res = reactive([]);

routes.forEach((route) => {

const tmp = { ...route };

if (tmp.children) {

let childrenBreadcrumb = [...breadcrumb];

childrenBreadcrumb.push(route);

let tmpRoute = { ...route };

tmpRoute.meta.breadcrumb = childrenBreadcrumb;

delete tmpRoute.children;

res.push(tmpRoute);

let childrenRoutes = flatAsyncRoutes(tmp.children, childrenBreadcrumb);

childrenRoutes.map((item) => {

res.push(item);

});

} else {

let tmpBreadcrumb = [...breadcrumb];

tmpBreadcrumb.push(tmp);

tmp.meta.breadcrumb = tmpBreadcrumb;

res.push(tmp);

}

});

return res;

}

//过滤树

function treeFilter(tree, func) {

return tree

.map((node) => ({ ...node }))

.filter((node) => {

node.children = node.children && treeFilter(node.children, func);

return func(node) || (node.children && node.children.length);

});

}
export default router;

2. 动态路由dynamic.ts

在动态路由dynamic.ts文件增加模拟动态路由内容。

dynamic.ts内容如下:

const routes = [
{
name: "home",
path: "/home",
meta: {
title: "首页",
icon: "ElemeFilled",
type: "menu",
},
children: [
{
name: "dashboard",
path: "/dashboard",
meta: {
title: "控制台",
icon: "Menu",
affix: true,
},
component: "home/index",
},
{
name: "directive",
path: "/other/directive",
meta: {
title: "指令",
icon: "Flag",
},
component: "other/directive",
},
{
path: "https://baidu.com",
name: "外链百度",
meta: {
title: "外链百度",
icon: "Link",
type: "link",
},
},
{
path: "/other/fullpage",
name: "fullpage",
meta: {
title: "整页路由",
icon: "Monitor",
fullpage: true,
hidden: true,
type: "menu",
},
component: "other/fullpage",
},
],
},
{
path: "/other/about",
name: "about",
meta: {
title: "关于",
icon: "InfoFilled",
type: "menu",
},
component: "other/about",
},
];
export default routes;

3. 在main.ts文件注册router

import router from "./router";
app.use(ElementPlus).use(store).use(router).mount("#app");

vite+vue3+ts+elementPlus前端框架搭建 [三] router路由管理的更多相关文章

  1. 全面解析JavaScript的Backbone.js框架中的Router路由

    这篇文章主要介绍了Backbone.js框架中的Router路由功能,Router在Backbone中相当于一个MVC框架中的Controller控制器功能,需要的朋友可以参考下. Backbone ...

  2. 基 vue-element-admin升级的Vue3 +TS +Element-Plus 版本的后端管理前端解决方案 vue3-element-admin 正式对外发布,有来开源组织又一精心力作,毫无保留开放从0到1构建过程

    项目简介 vue3-element-admin 是基于 vue-element-admin 升级的 Vue3 + Element Plus 版本的后台管理前端解决方案,是 有来技术团队 继 youla ...

  3. Unity 游戏框架搭建 (三) MonoBehaviour单例的模板

      上一篇文章讲述了如何设计C#单例的模板.也随之抛出了问题: 如何设计接收MonoBehaviour生命周期的单例的模板? 如何设计? 先分析下需求:   1.约束脚本实例对象的个数.   2.约束 ...

  4. ASP.NET Core微服务+Tabler前端框架搭建个人博客1--开始前想说的话

    写在前面 本人为在读研究生,特别喜欢.NET,觉得.NET的编程方式.语法都特别友好,学习.NET Core已经差不多有一年半了,从一开始不知道如何入门到现在终于可以编写一些小的应用程序,想一想还是非 ...

  5. 手把手教你使用VUE+SpringMVC+Spring+Mybatis+Maven构建属于你自己的电商系统之vue后台前端框架搭建——猿实战01

            猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...

  6. .Net Core3.0 WebApi 项目框架搭建 三:读取appsettings.json

    .Net Core3.0 WebApi 项目框架搭建:目录 appsettings.json 我们在写项目时往往会把一些经常变动的,可能会变动的参数写到配置文件.数据库中等可以存储数据且方便配置的地方 ...

  7. OnsenUI 前端框架(三)

    上一章咱们学习了OnsenUI的工具栏.侧边栏和标签栏.通过对页面上这三部分的学习,咱们对混合应用的一个页面有了大体上的认识.从这一章开始,咱们学习OnsenUI混合项目开发过程中会用到的各种各样的组 ...

  8. 自定义统一api返回json格式(app后台框架搭建三)

    在统一json自定义格式的方式有多种:1,直接重写@reposeBody的实现,2,自定义一个注解,自己去解析对象成为json字符串进行返回 第一种方式,我就不推荐,想弄得的话,可以自己去研究一下源码 ...

  9. ASP.NET Core微服务+Tabler前端框架搭建个人博客2--系统架构

    功能分析 在整个微服务架构的搭建过程中,我们需要做的第一步就是对服务进行拆分,将一个完整的系统模块化,通过对各个模块互联,共同完成一个系统的工作.既然要做到模块化,那么必须明白你的系统的需求到底是什么 ...

  10. VUE3企业级项目基础框架搭建流程(1)

    开发环境和技术栈 操作系统 windows11 开发工具 vscode.phpstudy(小皮):nginx1.15.11, mysql5.7.26, php7.4,Navicat for MySQL ...

随机推荐

  1. 阿里云数据库开源重磅发布:PolarDB HTAP的功能特性和关键技术

    简介:在3月2日的阿里云开源 PolarDB 企业级架构发布会上,阿里云 PolarDB 内核技术专家严华带来了主题为<PolarDB HTAP详解>的精彩演讲.在PolarDB存储计算分 ...

  2. [FAQ] FastAdmin epay 微信公众号支付 JSAPI 支付必须传 openid ?

    使用 FastAdmin 的 epay 插件时,我们通过传不同的 method 决定支付方式. method=mp 时表示公众号支付,此时必须要 openid,但是插件里并没有说明如何获取. 其实这个 ...

  3. MAUI 已知问题 PathFigureCollectionConverter 非线程安全

    在 MAUI 里,可以使用 PathFigureCollectionConverter 将 Path 字符串转换为 PathFigureCollection 对象,从而实现从 Path 字符串转换为路 ...

  4. Codeforces Round 940 (Div. 2) and CodeCraft-23 (A-E)

    A. Stickogon 题意:给定 \(n\) 根木棒长度,问最多构成几个多边形. 贪心,四边形不会优于三角形. submission B. A BIT of a Construction 题意:构 ...

  5. Solution Set - NOI真题

    NOI2024 RP++! NOI2018 Day1T1 Link&Submission. 考虑一个最高的水位线使所有点通过没有积水的边就可以连通,也就是求出了一棵海拔的最大生成树.会发现只有 ...

  6. THUWC2024&NOIWC2024游记

    以 NOIWC 考试日为 Day 1 好了. Day -6 到重庆了.去报到,然后直接不去试机走了,这波主打一个自信. Day -5 THUWC Day1,四道传统题. 开 T1,一眼有一个 \(O( ...

  7. WEB服务与NGINX(16)-网站logo之favicon.ico文件

    目录 1. 网站logo之favicon.ico文件 1.1 favicon.ico文件的作用 1.2 favicon.ico文件带来的问题 1. 网站logo之favicon.ico文件 1.1 f ...

  8. WEB服务与NGINX(12)-NGINX的变量

    目录 1. nginx的变量 1.1 内置变量 1.2 自定义变量 1. nginx的变量 nginx的变量可以在配置文件中引用,作为功能判断或日志等场景使用,变量可以分为内置变量和自定义变量. 内置 ...

  9. python教程2:if...else...+循环

    一.if判断 有单分支.双分支.多分支,下面就是一个多分支的案例: 二.缩进 三.for循环 四.while循环  五.其他 random模块  string模块

  10. C数据结构:KMP算法详解(呕心沥血)

    KMP算法 作者心声 了解暴力求解(必需会) KMP算法详解 记住我这段话(你会爱上它的)← : ①前后缀及其用处 ②求出前后缀的next数组 求出next数组的代码 开始实现KMP算法 结尾 附上源 ...