vue3.0项目实战系列文章 - 菜单的实现及icon组件封装

目录

一、先看下实现效果,然后注意几点

 二、二次封装图标组件

三、menu文件


一、先看下实现效果,然后注意几点

  • 图标是动态变化的,需要二次封装组件实现
  • 写法与vue2有所区别 

 二、二次封装图标组件

  • 图标是动态变化的,需要二次封装组件实现 
  • 注意引入的版本及写法,我引入的是"element-plus": "^2.2.13",以作参考

1.路径:src>components>modules>icon.vue

2.main.js引入,注意与vue2.0有所区别

//全局使用方法  vue2.0:Vue.component('el-icons', icons)
//图标
import icons from '@/components/modules/icon.vue'
app.component('el-icons', icons)   //使用时候元素的名称,进行注册的组件名

 3.icon.vue

<template>
  <el-icon :size="size" :color="color">
    <component :is="name"></component>
  </el-icon>
</template>

<script>
import { ref, defineComponent } from "vue";
import * as Icons from '@element-plus/icons-vue'
// import * as ElementPlusIconsVue from '@element-plus/icons-vue'
export default defineComponent({
  components: Icons,
  name: "ElIcons",
  props: {
    name: {
      type: String,
      required: true,
    },
    size: {
      type: String,
      default: "",
    },
    color: {
      type: String,
      default: "",
    },
  },
});
</script>

4.使用

<el-icons :name="item.icon"  />

三、menu文件

1.源文件

<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" text-color="#333333"
            active-text-color="#20a0ff" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template #title>
                            <el-icons :name="item.icon"  />
                            <span>{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs" :key="subItem.index">
                            <el-submenu v-if="subItem.subs" :index="subItem.index">
                                <template #title>{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">
                                    {{ threeItem.title }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index">{{ subItem.title }}
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                            <el-icons :name="item.icon"  />
                        <template #title>{{ item.title }}</template>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script lang="ts" setup>
    import {
        computed,
        watch
    } from "vue";
    import {
        useStore
    } from "vuex";
    import {
        useRoute
    } from "vue-router";

    const items = [{
            icon: "HomeFilled",
            index: "/role",
            title: "角色管理",
        }, {
            icon: "Menu",
            index: "/menu",
            title: "菜单管理",
        },
        {
            icon: "Tools",
            index: "/jurisdiction",
            title: "权限管理",
        },
    ] as any[];

    const route = useRoute();

    const onRoutes = computed(() => {
        return route.path;
    });

    const store = useStore();
    const collapse = computed(() => store.state.collapse);
</script>

<style scoped>
    .sidebar {
        display: block;
        position: absolute;
        left: 0;
        top: 70px;
        bottom: 0;
        overflow-y: scroll;
    }

    .sidebar::-webkit-scrollbar {
        width: 0;
    }

    .sidebar-el-menu:not(.el-menu--collapse) {
        width: 250px;
    }

    .sidebar>ul {
        height: 100%;
    }
</style>

修改样式:创建elementUi.css

main.js引入

 elementUi.css:

/* 导航菜单 */
/* 移动背景色 */
.el-menu-item:hover {
  background-color: #CBE6F5 !important;
  color: #20a0ff!important;
}
.el-menu-item:hover i {
  color: #20a0ff!important;
}
.el-menu-item i {
  color: #909399;
  margin-right: 5px;
  width: 24px;
}
/* 子菜单 */
.el-submenu .el-menu-item {
  background-color: #F9F9F9;
  color: #666666!important;
}
.el-menu-item.is-active{
  background-color: #CBE6F5;
  color: #20a0ff!important;
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3.0中的组件高级功能包括保持动态组件状态和引用DOM元素。 保持动态组件状态的方法是使用Vue内置的<keep-alive>组件。通过将动态组件包裹在<keep-alive>标签中,可以在切换动态组件时保持组件的状态。例如: ```html <keep-alive> <component :is="comName"></component> </keep-alive> ``` 在这个例子中,comName是一个data属性,用于指定当前要渲染的组件的名称。通过在<component>标签中使用:is属性,可以动态地指定要渲染的组件。 引用DOM元素的方法是使用ref属性。通过给DOM元素添加ref属性,可以在组件中通过$refs属性获取对DOM元素的引用。例如: ```html <template> <h3>MyRef组件</h3> <button @click="getRef">获取$refs引用</button> </template> <script> export default { methods: { getRef() { console.log(this.$refs) // $refs指向一个空对象,需要在DOM元素上添加ref属性 } } } </script> ``` 在这个例子中,点击按钮后,调用getRef方法可以在控制台上输出当前组件实例对象this。通过this.$refs可以访问到DOM元素的引用。 以上就是Vue3.0中组件高级功能的两个示例,分别是保持动态组件状态和引用DOM元素。希望能对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3.0----组件高级【下】(第五章)](https://blog.csdn.net/QQ675396947/article/details/127486948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BMG-Princess

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值