vue3 中使用 icon 图标的 3 种方案

1. element-icon

Element Plus 提供了一套常用的图标集合。

1.1. 安装

# 选择一个你喜欢的包管理器
# NPM
$ npm install @element-plus/icons-vue
# Yarn
$ yarn add @element-plus/icons-vue
# pnpm
$ pnpm install @element-plus/icons-vue# 选择一个你喜欢的包管理器

1.2. 注册所有图标

从 @element-plus/icons-vue 中导入所有图标并进行全局注册。

// main.ts
// 如果您正在使用CDN引入,请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

1.3. 基础用法

<!-- 使用 el-icon 为 SVG 图标提供属性 -->
<template>
  <div>
    <el-icon :size="size" :color="color">
      <Edit />
    </el-icon>
    <!-- 或者独立使用它,不从父级获取属性 -->
    <Edit />
  </div>
</template>

如果你想用字符串的形式,可以这样搞。

以侧边栏的图标渲染为例子。

我的路由是这样写的:

{
  path: '/index',
  name: 'Index',
  component: () => import('@/views/workbench/home/index.vue'),
  meta: {
  title: '工作台',
  icon: 'HomeFilled',
  affix: true,
}

当在组件中渲染的时候可以用 component 组件来渲染:

<el-menu-item
  :index="subItem.path"
  @click="handleClickMenu(subItem)"
  >
   <el-icon>
     <component :is="subItem.meta.icon"></component>
  </el-icon>
</el-menu-item>

最终效果:

2. svg-icon

如果 element 的 icon 不满足我们的需求的话,我们可以从 iconfont 去下载svg图标。然后使用。

2.1. 安装

首先要使用 vite-plugin-svg-icons 插件

yarn add vite-plugin-svg-icons -D
# or
npm i vite-plugin-svg-icons -D
# or
pnpm install vite-plugin-svg-icons -D

2.2. 配置

在 vite.config.ts 中配置一下

import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
export default () => {
  return {
    plugins: [
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
  }
}

这里注意 iconDirs 的路径,是读取的 svg icon 存放的目录。

2.3. 封装

我们把 svg 封装成一个组件:

<template>
  <i :class="['el-icon', spin && 'svg-icon-spin']" :style="getStyle">
    <svg aria-hidden="true">
      <use :xlink:href="symbolId" rel="external nofollow"  :fill="color" />
    </svg>
  </i>
</template>
<script setup lang="ts" name="SvgIcon">
  import { computed } from 'vue'
  import type { CSSProperties } from 'vue'
  const props = defineProps({
    prefix: {
      type: String,
      default: 'icon',
    },
    name: {
      type: String,
      required: true,
    },
    color: {
      type: String,
      default: '#ffffff',
    },
    size: {
      type: [Number, String],
      default: 20,
    },
    spin: {
      type: Boolean,
      default: false,
    },
  })
  const symbolId = computed(() => `#${props.prefix}-${props.name}`)
  const getStyle = computed((): CSSProperties => {
    const { size } = props
    let s = `${size}`
    s = `${s.replace('px', '')}px`
    return {
      fontSize: s,
    }
  })
</script>
<style scoped lang="scss">
  .el-icon {
    --color: inherit;
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1em;
    height: 1em;
    font-size: inherit;
    line-height: 1em;
    color: var(--color);
    fill: currentColor;
    svg {
      width: 1em;
      height: 1em;
    }
  }
  .svg-icon-spin {
    animation: circle 1.5s infinite linear;
  }
  /* 旋转动画 */
  @keyframes circle {
    0% {
      transform: rotate(0);
    }
    100% {
      transform: rotate(360deg);
    }
  }
</style>

这里我封装的支持 prefix、name、size、color、spin(是否旋转)等属性。

2.4. 使用

我们先去 ​​​​​​​iconfont 下载一个 svg 格式的图标。

下载完成后,把这个 svg 放入 iconDirs 声明的路径下面即可:

在项目中使用。引入这个组件然后使用即可。注意 name 跟你的 svg name 保持一致。

<SvgIcon name="welcome" size="400px" /><SvgIcon name="welcome" size="400px" />

我这里的图标效果是这样的:

3. iconify

iconify 是一种非常广泛的图标解决方案,它收集了全网所有的图标。

3.1. 安装

pnpm install --save-dev @iconify/vuepnpm install --save-dev @iconify/vue

3.2. 封装

import { h, defineComponent } from 'vue'
import { Icon as IconifyIcon } from '@iconify/vue'
export default defineComponent({
  name: 'IconifyIconOnline',
  components: { IconifyIcon },
  props: {
    icon: {
      type: String,
      default: '',
    },
  },
  render() {
    const attrs = this.$attrs
    return h(
      IconifyIcon,
      {
        icon: `${this.icon}`,
        style: attrs?.style
          ? Object.assign(attrs.style, { outline: 'none' })
          : { outline: 'none' },
        ...attrs,
      },
      {
        default: () => [],
      },
    )
  },
})

3.3. 使用

首先我们要找一个图标,可以去 icon-sets.iconify.design/ 搜索你想要的图标。

复制图标的名字。

在项目中直接使用

<template>
  <div class="btn">
    <el-tooltip content="刷新">
      <el-button circle>
        <IconifyIcon icon="ri:refresh-line" height="16" />
      </el-button>
    </el-tooltip>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { IconifyIcon } from '@/components/IconifyIcon'
export default defineComponent({
  components: {
    IconifyIcon,
  },
})
</script>
<style scoped lang="scss">
.btn {
  margin-right: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

如果你想直接在 vscode 中预览这个图标长啥样,就像下面这样:

你可以安装一个插件:Iconify IntelliSense

我们在浏览器中打开调试工具,看看 application ,发现这里缓存的一些图标

当第一次请求后,浏览器会把这个图标缓存。下次请求的时候直接从缓存中读取的。

如果您忘记了 Mobaxterm 的主密码,这可能会导致您无法访问由密码保护的文件和目录,或者在使用其他功能时遇到问题。以下是您可以尝试的一些方法。 1. 重新安装 Mobaxterm:如果您忘记了主密码,并且没有重要的数据需要保留,最简单的方法可能是卸载 Mobaxterm 并重新安装。这将重置所有设置,包括密码。 2. 重置主密码:如果您不想重新安装 Mobaxterm,并且您有管理员权限,您可以尝试使用以下步骤重置主密码。 a. 在 Mobaxterm 单击 "Settings"(设置) > "Master password"(主密码)。 b. 在主密码窗口,单击 "Reset"(重置)。 c. 您将被要求输入您的 Windows 登录密码,然后单击 "OK"(确定)。 d. Mobaxterm 将提示您输入新的主密码。 请注意,这将重置所有使用主密码保护的文件和目录。请确保备份所有重要的数据。 3. 使用备份文件:如果您已创建了 Mobaxterm 的备份文件(.mobaxterm),您可以使用该文件来恢复主密码。 a. 在 Mobaxterm 单击 "Settings" > "Configuration settings"(配置设置)。 b. 单击 "Backup and restore"(备份和恢复)。 c. 单击 "Restore settings"(恢复设置)并选择您的备份文件。 d. 在恢复设置向导的 "Security"(安全)选项卡,单击 "Change master password"(更改主密码)。 e. 输入新的主密码并单击 "OK"。 请注意,这将使用备份文件恢复所有设置,包括主密码。这也将覆盖您的当前设置。 总之,如果您忘记了 Mobaxterm 的主密码,您可以尝试重新安装它,重置密码或使用备份文件来恢复密码。请记住,在执行任何操作之前,请备份所有重要数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值