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

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. [Contract] Solidity 合约使用 truffle 部署到测试网和主网

    使用 truffle 发布到非本地的以太坊主网或者测试网时,需要提供钱包的助记词或私钥. 首先安装 truffle 组件:npm install @truffle/hdwallet-provider ...

  2. IIncrementalGenerator 获取引用程序集的所有类型

    本文告诉大家如何在使用 IIncrementalGenerator 进行增量的 Source Generator 生成代码时,如何获取到当前正在分析的程序集所引用的所有的程序集,以及引用的程序集里面的 ...

  3. dotnet 读 WPF 源代码笔记 为什么加上 BooleanBoxes 类

    在 WPF 框架,为什么需要定义一个 BooleanBoxes 类.为什么在 D3DImage 的 Callback 方法里面,传入的是 object 对象,却能被转换为布尔.本文将告诉大家为什么需要 ...

  4. k8s管理应用

  5. SpringBoot3.1.5对应新版本SpringCloud开发(1)-Eureka注册中心

    服务的提供者和消费者 服务之间可以通过Spring提供的RestTemplate来进行http请求去请求另一个Springboot的项目,这就叫做服务间的远程调用. 当一个服务通过远程调用去调用另一个 ...

  6. 鸿蒙HarmonyOS实战-ArkUI事件(单一手势)

    一.单一手势 应用程序的手势操作是指在移动设备上使用手指或手势进行与应用程序交互的方式.手势操作可以包括点击.滑动.双击.捏合等动作,用于实现不同的功能和操作. HarmonyOS中常见的手势操作及其 ...

  7. 网络性能监测与诊断的专家-AnaTraf

    网络性能问题是困扰许多企业和组织的常见问题.网络速度慢.延迟高.丢包率高.应用卡顿等问题都会严重影响用户体验和工作效率.为了解决这些问题,企业需要对网络流量进行分析和诊断,找出问题根源并采取措施进行优 ...

  8. 那什么是URL、URI、URN?

    URI Uniform Resource Identifier 统一资源标识符 URL Uniform Resource Locator 统一资源定位符 URN Uniform Resource Na ...

  9. 用 C 语言开发一门编程语言 — 语法解析器

    目录 文章目录 目录 前文列表 编程语言的本质 词法分析 语法分析 使用 MPC 解析器组合库 安装 快速入门 实现波兰表达式的语法解析 波兰表达式 正则表达式 代码实现 前文列表 <用 C 语 ...

  10. docker/k8s常见错误处理

    启动docker失败,报错了 启动docker失败,报错了.Failed to load environment files: No such file or directory [root@mcwk ...