当前位置: 首页 > news >正文

Vue3 自动引入组件及函数、动态生成侧边栏路由

Vue3 自动引入组件及函数、动态生成侧边栏路由

1、安装依赖

npm install -D unplugin-auto-import unplugin-icons unplugin-vue-components

插件使用说明

unplugin-auto-import 说明 —— 自动引入函数、组件

unplugin-vue-components 说明 —— 自动注册组件

unplugin-icons 说明 —— 自动安装、注册图标

2、vite.config.ts 配置文件

import {fileURLToPath, URL} from 'node:url';
import {defineConfig} from 'vite';
import vue from '@vitejs/plugin-vue';// 自动导入插件
// https://github.com/antfu/unplugin-auto-import
import AutoImport from 'unplugin-auto-import/vite';
// https://github.com/antfu/unplugin-vue-components
import Components from 'unplugin-vue-components/vite';
import {ElementPlusResolver} from "unplugin-vue-components/resolvers";
// https://github.com/antfu/unplugin-icons
import Icons from 'unplugin-icons/dist/vite';
import IconsResolver from 'unplugin-icons/dist/resolver';
import {FileSystemIconLoader} from "unplugin-icons/loaders";// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(),AutoImport({dirs: ['src', "src/**"],// 自动导入 Vue 相关函数,如 ref、reactive、toRef 等imports: ['vue', 'pinia', 'vue-router'],resolvers: [// 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)ElementPlusResolver(),// 自动解析 ElementPlus 图标组件IconsResolver({prefix: 'i',enabledCollections: ['ep'],// 识别自定义图标集customCollections: ['zj']})],dts: 'src/auto-import.d.ts'}),Components({// 要搜索组件的目录的相对路径。默认 ['src/components']dirs: ['src'],// 组件的有效文件扩展名。extensions: ['vue'],// 搜索子目录deep: true,// 生成dts文件dts: 'src/components.d.ts',// 指定项目 vue 版本version: 3,resolvers: [// 自动导入 Element Plus 组件ElementPlusResolver(),// 自动注册图标组件IconsResolver({prefix: 'i',enabledCollections: ['ep'],// 识别自定义图标集customCollections: ['zj']}),],}),Icons({compiler: 'vue3',// 自动安装图标集autoInstall: true,// 自定义图标加载customCollections: {// 定义一个名为 zj 的图标合集(element-ui的图标合集名称为 ep// 使用形式 i-zj-svg文件名zj: FileSystemIconLoader('src/assets/icon'),// 给svg文件设置fill="currentColor"属性,使图标的颜色具有适应性// home: FileSystemIconLoader('src/assets/svg/home', svg => svg.replace(/^<svg /, '<svg fill="currentColor" '))}})],resolve: {alias: {'@': fileURLToPath(new URL("./src", import.meta.url))}}
})

tips:可以不用安装图标库,unplugin-icons 的 autoInstall 配置项可以自动安装使用到的图标库

图标来源:[iconify](Icon Sets • Iconify)

3、动态注册侧边栏图标

<template><el-icon v-for="(item, i) in obj" :key="i"><component :is="item.icon"/></el-icon>
</template>
<script setup lang="ts">
// 不用引入直接使用
let obj = [{icon: IEpPlus},{icon: IEpMinus},{icon: IEpHouse},{icon: IEpDelete}
]
</script>
<style scoped>
</style>

4、侧边栏图标

// 自动导入图标
// https://www.cnblogs.com/lovewhatIlove/p/17201904.html
const ROUTE_ICON = {IEpFold: IEpMenu,IZjSystemOption: IZjSystemOption,
}
........
// ---------------------- 系统管理 -------------------------
{path: 'system',name: "system",redirect: '/home/system/permission',meta: {title: "系统管理",icon: ROUTE_ICON.IZjSystemOption},children: [{path: 'permission',name: 'permission',component: () => import("@/views/system/permission/index.vue"),meta: {title: "权限管理",icon: ROUTE_ICON.IEpFold}},{path: 'role',name: 'role',component: () => import("@/views/system/role/index.vue"),meta: {title: "角色管理",icon: ROUTE_ICON.IZjSystemOption}}]
},
http://www.lryc.cn/news/40879.html

相关文章:

  • 人工智能交互系统界面设计
  • 蓝桥杯嵌入式第一课--创建工程
  • Java面向对象:接口的学习
  • 西瓜视频登录页面
  • 【springboot】常用快捷键:
  • 宝塔控制面板常用Linux命令大全
  • C语言实现单链表(超多配图,这下不得不学会单链表了)
  • SQL编写优化技巧
  • 【基础算法】单链表的OJ练习(6) # 复制带随机指针的链表 #
  • Activity生命周期完成EvenetLog回调
  • 西安石油大学C语言期末真题实战
  • 【Shell】Shell变量
  • 你是真的“C”——结构体中鲜有人知的“秘密”
  • 2023年“网络安全”赛项江苏省淮安市赛题解析(超详细)
  • 【二分查找】
  • Vue学习 -- 如何用Axios发送请求(get post)Promise对象 跨域请求问题
  • TVS和稳压管的相同点和不同点
  • 微信小程序项目实例——扫雷
  • 2022-2023年度广东省职业院校学生专业技能大赛 中职组网络安全赛项竞赛规程
  • 超详细的堆排序,进来看看吧。
  • 线性回归 特征扩展的原理与python代码的实现
  • 订阅关系一致
  • 测试老鸟都在用的接口抓包常用工具以及接口测试工具都有哪些?
  • Delphi 一个函数实现腾讯云最新版(API3.0)短信发送
  • 2023年Android现代开发
  • 自然语言处理(NLP)在医疗领域的应用
  • 计算机中的浮点数运算
  • 看了字节跳动月薪20K+测试岗面试题,让我这个工作3年的测试工程师,冷汗直流....
  • 这两天最好的ChatGPT应用;使用Notion AI提升效率的经验(13);AI编程与程序员的生存 | ShowMeAI日报
  • Linux 内核likely与unlikey