vue3 + vite + antdv 项目中自定义图标

前言:

iconfont-阿里巴巴矢量图标库 下载自己需要的icon图标,下载格式为svg;项目中在存放静态资源的文件夹下 assets 创建一个存放svg格式的图片的文件夹。

步骤:

1、安装vite-plugin-svg-icons

javascript 复制代码
npm i vite-plugin-svg-icons -D

2、创建svgIcon.vue组件

javascript 复制代码
<template>
  <svg aria-hidden="true" :class="svgClass">
    <use :xlink:href="symbolId" :fill="props.color" />
  </svg>
</template>
<script>
import { defineComponent } from "vue";
export default defineComponent({
  name: "svg-icon",
});
</script>
<script setup>
import { computed } from "vue";
const props = defineProps({
  prefix: {
    type: String,
    default: "icon",
  },
  name: {
    type: String,
    require: true,
  },
  color: {
    type: String,
    default: "#333",
  },
  className: {
    type: String,
    default: "",
  },
});
const symbolId = computed(() => `#${props.prefix}-${props.name}`);
console.log(symbolId.value, "symbolIdsymbolId");
const svgClass = computed(() => {
  if (props.className) {
    return `svg-icon ${props.className}`;
  }
  return "svg-icon";
});
</script>

<style>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.1em;
  /* 因icon大小被设置为和字体大小一致,而span等标签的下边缘会和字体的基线对齐,故需设置一个往下的偏移比例,来纠正视觉上的未对齐效果 */
  fill: currentColor;
  /* 定义元素的颜色,currentColor是一个变量,这个变量的值就表示当前元素的color值,如果当前元素未设置color值,则从父元素继承 */
  overflow: hidden;
}
</style>

3、在main.js文件中全局注册

4、在vite.config.js文件中进行配置

5、在其他组件中使用

效果:

相关推荐
心连欣25 分钟前
解锁对象遍历:当字符串遇上for...in循环
前端·javascript
Sestid29 分钟前
前端Cursor使用指南(后续会更新Claude)
前端·claude·cursor
戴维南35 分钟前
LangChain 在 Agent 开发中的定位:10 个模块(含代码对比,耳机售后案例)
前端
ouzz44 分钟前
使用纯canvas绘制一个掘金首页
前端·canvas
用户6957584491281 小时前
Vue 3 响应式系统:解构赋值与依赖收集的正确姿势
前端
乐乐同学yorsal1 小时前
一个 TypeScript 写的图片视频处理工具箱,吊打一切付费软件!
前端·命令行
账号已注销free1 小时前
Vue3项目中给组件命名的方式
vue.js
前端那点事1 小时前
VueUse 全面指南|Vue3组合式工具集实战
vue.js
lzhdim1 小时前
SQL 入门 10:SQL 内置函数:数值、字符串与时间处理
前端·数据库·sql
jstopo网站1 小时前
水厂水泵工作流程图canvas动画
前端·javascript