使用svg引用iconfont

iconfont-阿里巴巴矢量图标库iconfont-国内功能很强大且图标内容很丰富的矢量图标库,提供矢量图标下载、在线存储、格式转换等功能。阿里巴巴体验团队倾力打造,设计和前端开发的便捷工具https://www.iconfont.cn/

1. 新建项目

2. 在项目中生成在线代码

3. 选择Symbol,生成的js文件下载下来

4. 将上面的iconfont的js文件放在assets目录下

5. 在main.ts中引入

import "@/assets/iconfont/iconfont";

 6. 封装SvgIcon组件

 

<template>
  <svg v-bind="svgProperty">
    <use :xlink:href="iconClassName" v-bind="svgStyle" />
  </svg>
</template>
<script setup lang="ts">
import { computed } from "vue";
interface Props {
  iconClass: string;
  className?: string;
  size: string | number;
  color: string;
}
const props = withDefaults(defineProps<Props>(), {
  size: "24",
  color: "black",
});
const svgIcon = "svg-icon";
const iconClassName = computed(() => {
  return `#${props.iconClass}`;
});
const svgProperty = computed(() => {
  return {
    class: `${svgIcon} ${props.className}`,
    width: `${props.size}px`,
    height: `${props.size}px`,
  };
});
type Style = {
  size: string;
  fill: string;
};
const svgStyle = computed(() => {
  const obj: Style = {
    size: "",
    fill: "",
  };
  if (props.size) {
    obj.size = `${props.size}px`;
  }
  if (props.color) {
    obj.fill = props.color;
  }
  return obj;
});
</script>

7. 将组件注册为全局组件

 8. 使用SvgIcon组件

   <SvgIcon
        class-name="ml-3 cursor-pointer"
        color="red"
        icon-class="icon-shezhi"
        size="20"
      ></SvgIcon>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值