vue3+element-plus的侧边菜单栏的图标的动态渲染

没有检索到标题
本文介绍了如何在ElementPlus框架中使用Vue全局注册NavMenu导航菜单的图标,包括安装ElementPlusIconsVue,以及在组件模板中通过数组动态显示不同图标。

Element-Ui组件 NavMenu 导航菜单图标的具体使用

在这里插入图片描述

首先要安装依赖

npm install @element-plus/icons-vue

//如果你已经全局安装element-plus的话就不需要安装这个了,直接按照下面的步骤走就行了

在main.js中全局导入

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

通过循环注册icon

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

在组件内部使用,首先定义图标的数组

// 菜单图标
const icons = reactive([
  'User',
  'SwitchFilled',
  'Document',
  'MessageBox',
  'Location',
  'Shop',
  'Orange',
  'Suitcase',
  'VideoPlay'
])

在模板内进行使用

    <el-sub-menu index="1" v-for="(item, index) in menuList" :keys="item.id">
              <template #title>
                <el-icon>
                  <component :is="icons[index]"></component>
                </el-icon>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item :index="itemChild.id" v-for="(itemChild, indexChild) in item.children" :keys="itemChild.id">
                <el-icon>
                  <Location />
                </el-icon>
                <span> {{ itemChild.authName }}</span>
              </el-menu-item>


            </el-sub-menu>
<el-icon>
                  <component :is="icons[index]"></component>
                </el-icon>
### 使用 Vue3Element-Plus 构建平台界面的最佳实践 实现一个基于 Vue3Element-Plus 的平台界面设计,通常涉及以下几个核心方面: #### 1. 初始化项目 创建一个新的 Vue3 项目可以使用 `Vite` 或者官方脚手架工具。以下是通过 Vite 创建项目的命令: ```bash npm create vite@latest demo_project --template vue-ts cd demo_project npm install ``` 这一步会初始化一个支持 TypeScript 的 Vue3 项目[^3]。 安装必要的依赖项,包括 `element-plus` 及其样式文件: ```bash npm install element-plus axios dayjs ``` #### 2. 配置 Element-Plus 为了优化性能并减少打包体积,推荐按需加载组件而非全局导入整个库。可以通过 `unplugin-vue-components` 插件来自动完成这一过程。在 `vite.config.ts` 中配置如下内容: ```typescript import { defineConfig } from &#39;vite&#39; import vue from &#39;@vitejs/plugin-vue&#39; import AutoImport from &#39;unplugin-auto-import/vite&#39; import Components from &#39;unplugin-vue-components/vite&#39; import { ElementPlusResolver } from &#39;unplugin-vue-components/resolvers&#39; export default defineConfig({ plugins: [ vue(), AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), ], }) ``` 这样可以在不手动注册的情况下直接使用 Element-Plus 组件[^2]。 #### 3. 设计布局结构 典型的管理后台页面由顶部导航栏、侧边菜单以及主要内容区域组成。以下是一个简单的模板示例: ```html <template> <el-container class="layout"> <!-- 左侧菜单 --> <el-aside width="200px"> <Sidebar /> </el-aside> <!-- 主体部分 --> <el-container> <el-header>Header</el-header> <el-main> <router-view></router-view> </el-main> </el-container> </el-container> </template> <script lang="ts" setup> // 导入自定义的 Sidebar 组件或其他逻辑 import Sidebar from &#39;./components/Sidebar.vue&#39;; </script> <style scoped> .layout { height: 100vh; } .el-header { background-color: #b3c0d1; color: #fff; line-height: 60px; } .el-aside { background-color: #545c64; color: white; } </style> ``` #### 4. 表单交互与动态渲染 对于表单的设计,可以利用 Element-Plus 提供的功能强大的 `<el-form>` 组件。下面是一段用于上传图片的代码片段: ```vue <template> <el-form :model="formModel" label-width="80px"> <el-form-item label="封面图"> <el-upload action="#" :auto-upload="false" @change="onUploadFile"> <img v-if="imgUrl" :src="imgUrl" style="width: 100px;" /> <el-icon v-else><plus /></el-icon> </el-upload> </el-form-item> </el-form> </template> <script lang="ts" setup> import { ref, reactive } from &#39;vue&#39;; const formModel = reactive({ cover_img: null }); const imgUrl = ref(&#39;&#39;); function onUploadFile(uploadFile: any) { imgUrl.value = URL.createObjectURL(uploadFile.raw); formModel.cover_img = uploadFile.raw; } </script> ``` 上述代码展示了如何绑定图片预览功能,并将选中的文件存储到模型中以便后续提交给服务器[^4]。 #### 5. 路由设置 最后,在应用中集成路由模块以支持多页切换。编辑 `main.ts` 文件引入路由实例: ```typescript import { createApp } from &#39;vue&#39;; import App from &#39;./App.vue&#39;; import router from &#39;./router&#39;; // 假设已存在该目录下的 index.ts 文件 import &#39;element-plus/dist/index.css&#39;; createApp(App).use(router).mount(&#39;#app&#39;); ``` --- ### 总结 以上介绍了从零搭建基于 Vue3Element-Plus 平台界面的过程,涵盖了环境准备、框架配置、基础布局构建以及常见业务场景开发等内容。这些方法能够帮助开发者快速上手并高效完成复杂的企业级管理系统建设工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值