vue3使用svg图标的多种方式总结

 更新时间:2023年03月24日 09:49:30   作者:@小朱呀路  
svg图片在项目中使用的非常广泛,下面这篇文章主要给大家介绍了关于vue3使用svg图标的多种方式,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

方式1使用在线链接访问

在iconfont找到自己的项目的图标选择Symbol获取在线链接

2:在vue3项目中找到public的index.html进行script进行引入

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

在项目直接使用

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

显示出了删除的图标

封装的写法(上面的代码写着太重复下面进行封装)

1:新建一个专门获取svg图标的组件

icon.vue (svg/index.vue)

 <template>
  <div>
    <svg :style="style">
      <use :href="names" rel="external nofollow"  rel="external nofollow" ></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>

需要显示图标的界面

<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" rel="external nofollow"  rel="external nofollow" ></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>

总结 

到此这篇关于vue3使用svg图标的多种方式总结的文章就介绍到这了,更多相关vue3使用svg图标内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • VUE 实现动态给对象增加属性,并触发视图更新操作示例

    VUE 实现动态给对象增加属性,并触发视图更新操作示例

    这篇文章主要介绍了VUE 实现动态给对象增加属性,并触发视图更新操作,涉及vue.js对象属性动态操作及视图更新相关实现技巧,需要的朋友可以参考下
    2019-11-11
  • Vue2+JS实现扫雷小游戏

    Vue2+JS实现扫雷小游戏

    实现小游戏可以锻炼自己的逻辑思维能力,也不会很枯燥,完成时会给自己带来一种满足感。本文就将利用Vue和JS实现简单的扫雷小游戏,需要的可以参考一下
    2022-06-06
  • vue3 typescript封装axios过程示例

    vue3 typescript封装axios过程示例

    这篇文章主要为大家介绍了vue3 typescript封装axios过程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • vue-element-admin登录拦截设置白名单方式

    vue-element-admin登录拦截设置白名单方式

    这篇文章主要介绍了vue-element-admin登录拦截设置白名单方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • element表格变化后自动刷新的两种方案

    element表格变化后自动刷新的两种方案

    本文主要介绍了element表格变化后自动刷新的两种方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Vue报错Syntax Error:TypeError: this.getOptions is not a function的解决方法

    Vue报错Syntax Error:TypeError: this.getOptions is not a 

    前几天在vue运行项目过程中报错了,所以下面这篇文章主要给大家介绍了关于Vue报错Syntax Error:TypeError: this.getOptions is not a function的解决方法,需要的朋友可以参考下
    2022-07-07
  • react+ ts vite搭建及二次封装请求的过程解析

    react+ ts vite搭建及二次封装请求的过程解析

    这篇文章主要介绍了react+ ts vite搭建及二次封装请求,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • vue中如何获取当前路由地址

    vue中如何获取当前路由地址

    这篇文章主要介绍了vue中如何获取当前路由地址,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • Vue3中watchEffect高级侦听器的具体使用

    Vue3中watchEffect高级侦听器的具体使用

    Vue3中新增了一种特殊的监听器watchEffect,本文主要介绍了Vue3中watchEffect高级侦听器的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • vue-resource请求实现http登录拦截或者路由拦截的方法

    vue-resource请求实现http登录拦截或者路由拦截的方法

    这篇文章主要介绍了vue-resource请求实现http登录拦截或者路由拦截的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07

最新评论