vueRouter4.0+ts

准备工作

需要了解的接口

官方提供的路由对象数据类型

RouteRecordRaw

自定义定义路由对象数据类型

// Omit 删除指定类型的key返回删除后的接口类型
export interface AppRouteRecordRaw extends Omit<RouteRecordRaw, 'meta'> {
  name: string;
  meta: RouteMeta;
  component?: Component | string;
  components?: Component;//一个页面或者视图多个路由会使用
  children?: AppRouteRecordRaw[];
  props?: any;
  fullPath?: string;
}

路由目录结构

在这里插入图片描述
type 路由对象类型

type.ts

export interface RouteMeta {
  title: string;
}
export interface AppRouteRecordRaw extends Omit<RouteRecordRaw, 'meta'> {
  name: string;
  meta: RouteMeta;
  component?: Component | string;
  components?: Component;//一个页面或者视图多个路由会使用
  children?: AppRouteRecordRaw[];
  props?: any;
  fullPath?: string;
}
export type AppRouteModule = AppRouteRecordRaw;

routes/index.ts



import { RouteRecordRaw } from 'vue-router';
// import.meta.globEager 是vite提供的函数可以引入指定目录里面的所有指定类型文件返回数组
const modules = import.meta.globEager('./modules/**/*.ts');

const routeModuleList: RouteRecordRaw[] = [];

Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  // console.log('mod :>> ', mod);
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});


// 处理好数据后统一返回到 路由实例
export const basicRoutes:RouteRecordRaw[] =routeModuleList;

创建路由module

import { AppRouteModule } from "../../types";
const LoginRoute: AppRouteModule = {
    path: '/login',
    name: 'Login',
    component: () => import('/@/views/sys/login/Login.vue'),
    meta: {
      title: "登录",
    },
  };
export default LoginRoute;

创建路由实例

import type { App } from 'vue';
import type { RouteRecordRaw } from 'vue-router';
import { createRouter, createWebHashHistory } from 'vue-router';
import { basicRoutes } from './routes';
/**
 * 创建路由实例
 */
export const router = createRouter({
    history:createWebHashHistory(),
    routes:(basicRoutes as unknown) as RouteRecordRaw[]
});
/**
 * 路由拦截
 */
router.beforeEach((to,from)=>{
  console.log('to :>> ', to,from);
})
/**
 * 挂载到vue实例函数
 * @param app vue实例
 */
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;

Vue3是一种流行的JavaScript框架,它提供了用于构建用户界面的高效工具和组件。在使用Vue3时,结合TypeScript可以提供更好的类型检查和开发体验。Vue RouterVue框架的官方路由管理工具,能够轻松实现单页面应用的路由功能。 Pinia是Vue3官方推荐的状态管理工具。它使用类似于Vuex的store模式,但通过使用Vue3的响应性机制,提供了更好的性能和开发体验。 Element Plus是一套基于Vue3的UI组件库,它提供了丰富的功能和美观的界面,可以用于构建现代化的Web应用程序。 在搭建Vue3和TypeScript环境模板时,我们可以使用Vite这个工具。Vite是一个快速的Web开发工具,它基于ES模块的原生浏览器导入功能,能够提供快速的冷启动和热模块替换。我们可以使用Vite的模板来创建一个基于Vue3和TypeScript的项目骨架,以及集成Vue Router和Element Plus。 在进行Vite打包优化时,我们可以采取以下措施来提高打包效率和性能: 1. 使用按需加载:只加载项目所需的模块,减少打包体积和加载时间。 2. 配置代码分割:将代码拆分为多个小块,按需加载,提高页面加载速度。 3. 使用压缩和混淆工具:使用压缩和混淆工具来减小打包体积,提高加载速度。 4. 配置缓存策略:启用浏览器缓存,减少重复加载和网络请求。 5. 使用CDN加速:将一些公共资源,如Vue、Element Plus等引入CDN,提高加载速度。 6. 优化依赖管理:检查项目的依赖关系,合理管理和更新依赖,避免冗余和重复引入。 通过以上优化措施,我们可以提高项目的打包效率和性能,提供更好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值