位置: IT常识 - 正文

vue3使用svg图标多种方式(vue引用svg矢量图)

编辑:rootadmin
vue3使用svg图标多种方式

推荐整理分享vue3使用svg图标多种方式(vue引用svg矢量图),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue svga,vue中使用svg图标,vue3 svg,vue-svg-loader,vue中使用svg图标,vue svgicon,vue3 svg,vue3 svg,内容如对您有帮助,希望把文章链接给更多的朋友!

方式1使用在线链接访问 在iconfont找到自己的项目的图标选择Symbol获取在线链接 2:在vue3项目中找到public的index.html进行script进行引入

打开浏览器看:这样就会自动注入到body下

在项目直接使用

//控制图标的大小 <svg style="width: 10px; height: 10px"> <use href="#icon-shanchu"></use> </svg>

显示出了删除的图标

封装的写法(上面的代码写着太重复下面进行封装) 1:新建一个专门获取svg图标的组件 icon.vue (svg/index.vue)

<template> <div> <svg :style="style"> <use :href="names"></use> </svg> </div></template><script setup>import { defineProps, withDefaults } from "vue";const props = defineProps({ name: { type: String, default: "", }, style: { type: Object, default: () => { return { width: 10, height: 10, color: "", }; }, },});const names = `#${props.name}`;</script><style lang="scss" scoped></style>vue3使用svg图标多种方式(vue引用svg矢量图)

需要显示图标的界面

<template> <div class="home"> <icon :style="{ width: 10, height: 10, color: 'red' }" name="icon-shanchu" ></icon> <icon :style="{ width: 10, height: 10, color: 'red' }" name="icon-shanchu" ></icon> </div></template><script setup>import { ref } from "vue";import icon from "../assets/svg/index.vue";</script><style lang="scss"></style>

假如你既引用了iconfont的图标也自定义了图标:直接放在一起根据传输的name指定使用哪一个图标 icon.vue (svg/index.vue)

<template> <div> <svg :style="style"> <use :href="names"></use> </svg> // 自定义的图标 <svg width="0" height="0"> <defs> <symbol id="more" viewBox="0 0 100 100"> <circle r="5" cx="20" cy="25" fill="transparent" stroke="green" ></circle> <circle r="5" cx="20" cy="50" fill="currentColor"></circle> <circle r="5" cx="20" cy="75" fill="currentColor"></circle> <line x1="40" y1="25" x2="90" y2="25" stroke-width="8" stroke="currentColor" ></line> <line x1="40" y1="50" x2="90" y2="50" stroke-width="8" stroke="currentColor" ></line> <line x1="40" y1="75" x2="90" y2="75" stroke-width="8" stroke="currentColor" ></line> </symbol> </defs> </svg> </div></template><script setup>import { defineProps, withDefaults } from "vue";const props = defineProps({ name: { type: String, default: "", }, style: { type: Object, default: () => { return { width: 10, height: 10, color: "", }; }, },});const names = `#${props.name}`;</script><style lang="scss" scoped></style>

使用:

<template> <div class="home"> <icon :style="{ width: 10, height: 10, color: 'red' }" name="icon-shanchu" ></icon> <icon :style="{ width: 10, height: 10, color: 'red' }" name="icon-shanchu1" ></icon> <icon :style="{ width: 20, height: 20, color: 'red' }" name="more"></icon> </div></template><script setup>import { ref } from "vue";import icon from "../assets/svg/index.vue";</script><style lang="scss"></style>

假如你是复制的iconfont官网的图标svg的代码:

你直接cv到项目也可以直接使用:

<svg t="1673881805558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1076" width="200" height="200" > <path d="M658.276045 767.993958 658.276045 274.295l329.126 0L987.402045 219.44 658.276 219.44l0-18.281c0-80.787046-65.492992-146.284032-146.276045-146.284032-80.790016 0-146.276045 65.496986-146.276045 146.284032l0 18.281L36.597 219.44l0 54.855 109.695 0 0 694.83L877.7 969.125l0-548.55-54.855 0L822.845 914.27l-621.69 0L201.155 274.295l164.569 0 0 493.699 54.848 0L420.572 274.295l182.85 0 0 493.699L658.276 767.994zM420.571034 219.440026l0-18.281c0-50.492006 40.932966-91.420979 91.428966-91.420979 50.489037 0 91.420979 40.928973 91.420979 91.420979l0 18.281L420.571 219.440026z" p-id="1077" ></path> </svg>

效果如下:

我们还可以把上面的代码进行改造直接使用在 icon.vue (svg/index.vue)改造

<template> <div class="home"> <icon :style="{ width: 10, height: 10, color: 'red' }" name="icon-shanchu" ></icon> <icon :style="{ width: 10, height: 10, color: 'red' }" name="icon-shanchu1" ></icon> <icon :style="{ width: 20, height: 20, color: 'red' }" name="more"></icon> <svg t="1673881805558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1076" width="200" height="200" > <path d="M658.276045 767.993958 658.276045 274.295l329.126 0L987.402045 219.44 658.276 219.44l0-18.281c0-80.787046-65.492992-146.284032-146.276045-146.284032-80.790016 0-146.276045 65.496986-146.276045 146.284032l0 18.281L36.597 219.44l0 54.855 109.695 0 0 694.83L877.7 969.125l0-548.55-54.855 0L822.845 914.27l-621.69 0L201.155 274.295l164.569 0 0 493.699 54.848 0L420.572 274.295l182.85 0 0 493.699L658.276 767.994zM420.571034 219.440026l0-18.281c0-50.492006 40.932966-91.420979 91.428966-91.420979 50.489037 0 91.420979 40.928973 91.420979 91.420979l0 18.281L420.571 219.440026z" p-id="1077" ></path> </svg> //改造好了直接使用 <icon :style="{ width: 20, height: 20, color: 'red' }" name="icon"></icon> </div></template><script setup>import { ref } from "vue";import icon from "../assets/svg/index.vue";</script><style lang="scss"></style>

本文链接地址:https://www.jiuchutong.com/zhishi/293156.html 转载请保留说明!

上一篇:Vue实现生成二维码(vue生成二维码分享)

下一篇:【swinUnet官方代码测试自己的数据集(已训练完毕)】

  • 建筑服务增值税税率
  • 个人所得税定额扣除怎样理解
  • 申请电子发票需要交钱吗
  • 印花税购销合同计税依据70%
  • 所得税申报时成本数据填错怎么办
  • 微信怎么开公司账户
  • 增值税即征即退政策适用范围
  • 大型机械进退场费属于机械台班单价组成部分
  • 网银 密码器
  • 投资可赎回基金怎么入账?
  • 建设工程中税费如何承担
  • 注册资本的印花税
  • 增值税专用发票抵扣最新规定
  • 租车租赁费税率是多少
  • 营改增之前
  • 地下商铺出售产权归属
  • 网上报税申报成功之后该怎么操作
  • 资管公司如何缴纳流转税?
  • 五月份和六月份都有什么节日
  • 结转法定盈余公积会计分录怎么写
  • 小型微利企业的税收政策
  • linux使用cp
  • php数组函数面试题
  • php auth_http类库进行身份效验
  • 公司帮人代缴社保怎么做账
  • linux bt命令
  • 应付账款周转天数越大说明什么
  • php bcsub
  • 浅谈php技术
  • linux操作系统文件系统
  • fssm32.exe是什么进程 有什么作用 fssm32进程查询
  • php调用sql server存储过程
  • 建筑行业有哪些岗位,从事的要求有哪些
  • php对接第三方支付教程
  • PHP isset()与empty()的使用区别详解
  • php 进程间通信
  • 微信小程序云开发控制台
  • vue项目兼容ie9以上浏览器
  • phpcms教程
  • 金碟怎么初始化
  • 对公账户和私人账户怎么区分
  • 发票章与开票方名称不一致是什么情况
  • 要求供应商赔偿说明函范本
  • phpstudy配置php环境变量
  • 成本会计制造费用核算的内容
  • 社保代扣代缴的规定
  • 企业如何采购
  • 资源税申报怎么操作
  • SQL Server 2008+ Reporting Services (SSRS)使用USER登录问题
  • 现代服务业进项抵扣新政策
  • 记账凭证应具备的基本内容包括
  • 确认主营业务收入分录怎么写
  • 老板垫付货款
  • 企业所得税退税怎么操作
  • 月末一次结转销售成本分录
  • 税务局三代手续费是什么
  • 银行存款日记账与银行对账单之间的核对属于
  • 收到银行承兑汇票计入什么科目
  • 计提资产减值是好事还是坏事
  • 工业企业固定资产投资
  • 清空mysql数据库
  • centos7如何添加永久静态路由
  • 浪潮云是什么意思
  • windows mobile应用
  • windows mobile10
  • linux bzz
  • OpenGL Framebuffer Object (FBO)
  • jquery插件怎么用到自己的网站
  • 简单介绍linux系统有哪些主要特点?
  • js设置延时执行
  • node.js的安装步骤
  • unity3d2019安装步骤
  • java script入门
  • python系统代码
  • 基于javascript创建导航页面
  • 广西2023新农合报销政策
  • 房产税的计税依据与税率分别是什么
  • 广州车船税每年交多少
  • 小规模纳税人公司买车能抵多少税
  • 公司小规模怎么纳税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设