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>

最后效果:

相关文章
|
8月前
|
容器
echarts pie 文字格式改为可换行
echarts pie 文字格式改为可换行
845 0
|
JavaScript 前端开发 数据安全/隐私保护
vue3+ts+elementplus写一个登录页面教程
【6月更文挑战第3天】本文介绍了如何使用 Vue 3 和 TypeScript 创建一个登录页面。首先,需安装 Vue CLI,然后创建新项目并启用 TypeScript 支持。接着,创建 `Login.vue` 组件,设计登录表单,包括用户账号、密码和验证码字段,并实现相关验证规则。页面样式包括背景、登录框和按钮等元素的布局与样式。最后,展示了`&lt;script&gt;`部分的代码,包括表单验证逻辑、生成验证码的函数以及登录提交处理。文章还提供了一个登录页面的截图和完整代码示例。
6054 1
|
前端开发
前端ElementPlus框架中的几种图标按钮使用方式
本文介绍了如何在Element Plus前端框架中使用带有图标的按钮,包括设置按钮大小、图标大小、按钮类型以及如何为图标添加点击事件。
1658 0
|
JavaScript
Vue3警告提示(Alert)
这是一个基于 Vue 的警告提示组件 Alert,提供了成功、信息、警告和错误四种样式,并支持自定义内容、图标及操作项。
647 4
Vue3警告提示(Alert)
|
移动开发 API UED
|
JavaScript 关系型数据库 MySQL
node连接mysql,并实现增删改查功能
【8月更文挑战第26天】node连接mysql,并实现增删改查功能
620 3
|
JavaScript
Vue在子组件中判断父组件是否传来事件
本文介绍了在Vue中如何通过`vm.$listeners`对象来判断父组件是否传递了特定的事件给子组件,并展示了如何检查事件是否存在以及相应的处理方法。
828 0
Vue在子组件中判断父组件是否传来事件
element UI 中多行消息提示的实现(this.$message的换行)
element UI 中多行消息提示的实现(this.$message的换行)
1082 0
|
JavaScript
成功解决node、node-sass和sass-loader版本冲突问题、不需要降低node版本。如何在vue项目中安装node-sass,以及安装node-sass可能遇到的版本冲突问题
这篇文章介绍了在Vue项目中安装node-sass和sass-loader时遇到的版本冲突问题,并提供了解决这些问题的方法,包括在不降低node版本的情况下成功安装node-sass。
成功解决node、node-sass和sass-loader版本冲突问题、不需要降低node版本。如何在vue项目中安装node-sass,以及安装node-sass可能遇到的版本冲突问题
|
API
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
4274 1