Element Plus如何动态切换icon

简介: Element Plus如何动态切换icon

参考资料:

Element Plus最新图标引入以及使用方法,icon动态组件,点击切换图标_@element-plus/icons-vue

vue3 elementplus el-Icon 动态渲染 点击切换icon图标组件_vue3怎样实现点击切换图标?

这里要利用动态组件进行切换,使用一个component组件,之前用ELEMENT-UI想要实现动态切换的写法:这是之前的写法

由于ELEMENTPLUS不能通过改变类名的方式修改icon,这里如何切换呢?

把原先icon的图标用component进行替换

这里的最终解决方案是:利用v-show方法显示和隐藏解决的,先用click绑定一个方法,:is绑定你想要显示icon的内容,然后用v-if 和v-else

          <el-icon @click="handleCollapse">
            <component :is="Fold" v-if="isCollapse"></component>
            <component :is="Expand" v-else></component>
          </el-icon>

Script中一定要引入你的icon才可以

<script setup>
import {
    House,
    Menu,
    Fold
} from '@element-plus/icons-vue'
 
 
</script>

定义一个isCollapse判断显不显示,this顺序很重要,有时候点了一下没反应,按照下面的顺序

<script setup>
 
import { House, Menu, Fold, Expand, ArrowRight} from "@element-plus/icons-vue";
</script>
<script>
export default {
  name: "HomeView",
  data() {
    return {
      isCollapse: false,
      asideWidth: "200px",
      flag:true
    };
  },
  methods: {
    handleCollapse() {
      this.isCollapse = !this.isCollapse; 
      this.asideWidth = this.isCollapse ? "64px" : "200px";
    },
  },
};
</script>

这样就是实现切换了,用了component

附赠源码:

<template>
  <div>
    <el-container>
      <el-aside
        :width="asideWidth"
        style="min-height: 100vh; background-color: #001529"
      >
        <div
          style="
            height: 60px;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
          "
        >
          <!-- <img src="@/assets/logo.png" style="width: 60px;height: 10px;">
                    <span class="logo-title" v-show="!isCollapse">零一</span> -->
        </div>
        <el-menu
          :collapse="isCollapse"
          style="border: none"
          background-color="#001529"
          text-color="rgba(255,255,255,0.65)"
          default-active="$router.path"
        >
          <el-menu-item index="/">
            <el-icon>
              <House />
            </el-icon>
            <span>系统首页</span>
          </el-menu-item>
          <el-menu-item>
            <template #title>
              <el-icon>
                <House />
              </el-icon>
              <span>系统首页</span>
            </template>
          </el-menu-item>
          <el-menu-item>
            <template #title>
              <el-icon>
                <House />
              </el-icon>
              <span>系统首页</span>
            </template>
          </el-menu-item>
          <el-sub-menu index="1-4">
            <template #title>
              <el-icon>
                <Menu />
              </el-icon>
              <span>信息管理</span>
            </template>
            <el-menu-item index="1-4-1">用户信息</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <el-icon @click="handleCollapse">
            <component :is="Expand" v-if="isCollapse"></component>
            <component :is="Fold" v-else></component>
          </el-icon>
          <el-breadcrumb :separator-icon="ArrowRight" style="margin-left:20px">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/' }">用户管理</el-breadcrumb-item>
          </el-breadcrumb>
        </el-header>
        <el-main> </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<style scoped>
.el-menu--inline {
  background-color: #000c17 !important;
}
 
.el-menu--inline .el-menu-item {
  background-color: #000c17 !important;
}
 
.el-menu-item:hover,
.el-sub-menu__title:hover span {
  color: #fff !important;
}
 
.el-sub-menu__title:hover i {
  color: #fff !important;
}
 
.el-menu-item.is-active {
  background-color: #40a9ff !important;
  color: #fff;
  border-radius: 5px !important;
  margin: 4px !important;
  width: calc(100% -8px);
  margin-left: 4px;
}
 
.el-menu-item.is-active i,
.el-menu-item.el-menu-item.is-active .el-tooltip {
  margin-left: -4px;
}
 
.el-menu-item {
  height: 40px !important;
  line-height: 40px !important;
  margin: 4px !important;
}
 
.el-submenu__title {
  height: 40px !important;
  line-height: 40px !important;
  margin: 4px !important;
}
 
.el-aside {
  transition: width .3s;
  box-shadow: 2px 0 6px rgba(0,21,41,.35);
}
.el-header {
  box-shadow: 2px 0 6px rgba(0,21,41,.35);
  display: flex;
  align-items: center;
}
.el-aside div .img {
  width: 30px;
}
.el-aside div span {
  font-size: 14px;
  font-family: "Times New Roman", Times, serif;
  font-weight: 700;
}
.logo-title {
  margin-left: 5px;
  font-size: 20px;
  transition: all 0.3s;
}
</style>
<script setup>
 
import { House, Menu, Fold, Expand, ArrowRight} from "@element-plus/icons-vue";
</script>
<script>
export default {
  name: "HomeView",
  data() {
    return {
      isCollapse: false,
      asideWidth: "200px",
      flag:true
    };
  },
  methods: {
    handleCollapse() {
      this.isCollapse = !this.isCollapse; 
      this.asideWidth = this.isCollapse ? "64px" : "200px";
    },
  },
};
</script>

最后效果:

相关文章
|
JavaScript
Vue怎么通过路由实现页面的局部跳转
Vue怎么通过路由实现页面的局部跳转
477 0
|
前端开发 微服务
Element-Plus 表格 el-table 如何支持分页多选
Element-Plus 表格 el-table 如何支持分页多选
Element el-button 按钮组件详解
本文目录 1. 背景 2. 按钮分类 3. 按钮样式 4. 按钮状态 5. 按钮分组 6. 按钮尺寸 7. 小结
3586 0
Element el-button 按钮组件详解
|
缓存 网络协议 应用服务中间件
NATAPP - 连不上 / 错误信息等问题解决汇总
NATAPP - 连不上 / 错误信息等问题解决汇总
4207 0
NATAPP - 连不上 / 错误信息等问题解决汇总
|
前端开发
前端ElementPlus框架中的几种图标按钮使用方式
本文介绍了如何在Element Plus前端框架中使用带有图标的按钮,包括设置按钮大小、图标大小、按钮类型以及如何为图标添加点击事件。
1372 0
|
JavaScript 前端开发 数据安全/隐私保护
vue3+ts+elementplus写一个登录页面教程
【6月更文挑战第3天】本文介绍了如何使用 Vue 3 和 TypeScript 创建一个登录页面。首先,需安装 Vue CLI,然后创建新项目并启用 TypeScript 支持。接着,创建 `Login.vue` 组件,设计登录表单,包括用户账号、密码和验证码字段,并实现相关验证规则。页面样式包括背景、登录框和按钮等元素的布局与样式。最后,展示了`&lt;script&gt;`部分的代码,包括表单验证逻辑、生成验证码的函数以及登录提交处理。文章还提供了一个登录页面的截图和完整代码示例。
5478 1
|
JavaScript
Vue3警告提示(Alert)
这是一个基于 Vue 的警告提示组件 Alert,提供了成功、信息、警告和错误四种样式,并支持自定义内容、图标及操作项。
512 4
Vue3警告提示(Alert)
|
JavaScript 前端开发
Vue3动态面包屑的实现
Vue3动态面包屑的实现
482 0
|
JavaScript 前端开发 索引
Vue3 + Vite项目实战:常见问题与解决方案全解析
Vue3 + Vite项目实战:常见问题与解决方案全解析
1113 0
|
JavaScript 前端开发 数据安全/隐私保护
UI 框架:Element-plus组件库(一)
在现代Web开发中,用户界面的设计与交互体验至关重要。随着前端技术的迅速发展,各种UI框架层出不穷,旨在提升开发效率和用户体验。其中,Element Plus作为一款基于Vue 3的组件库,因其简洁优雅的设计和丰富的功能而备受欢迎。 Element Plus不仅提供了众多高质量的组件,还注重与开发者的友好互动,使得即使是初学者也能快速上手。在本系列文章中,我们将深入探讨Element Plus的各个组件及其应用,通过实例演示如何有效利用该框架构建美观且功能强大的用户界面。
1553 0

热门文章

最新文章