vue3使用svg圖標(biāo)的多種方式總結(jié)
方式1使用在線鏈接訪問(wèn)
在iconfont找到自己的項(xiàng)目的圖標(biāo)選擇Symbol獲取在線鏈接
2:在vue3項(xiàng)目中找到public的index.html進(jìn)行script進(jìn)行引入
打開(kāi)瀏覽器看:這樣就會(huì)自動(dòng)注入到body下
在項(xiàng)目直接使用
//控制圖標(biāo)的大小 <svg style="width: 10px; height: 10px"> <use href="#icon-shanchu" rel="external nofollow" ></use> </svg>
顯示出了刪除的圖標(biāo)
封裝的寫法(上面的代碼寫著太重復(fù)下面進(jìn)行封裝)
1:新建一個(gè)專門獲取svg圖標(biāo)的組件
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>
需要顯示圖標(biāo)的界面
<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的圖標(biāo)也自定義了圖標(biāo):直接放在一起根據(jù)傳輸?shù)膎ame指定使用哪一個(gè)圖標(biāo)
icon.vue (svg/index.vue)
<template> <div> <svg :style="style"> <use :href="names" rel="external nofollow" rel="external nofollow" ></use> </svg> // 自定義的圖標(biāo) <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>
假如你是復(fù)制的iconfont官網(wǎng)的圖標(biāo)svg的代碼:
你直接cv到項(xiàng)目也可以直接使用:
<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>
效果如下:
我們還可以把上面的代碼進(jìn)行改造直接使用在 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>
總結(jié)
到此這篇關(guān)于vue3使用svg圖標(biāo)的多種方式總結(jié)的文章就介紹到這了,更多相關(guān)vue3使用svg圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE 實(shí)現(xiàn)動(dòng)態(tài)給對(duì)象增加屬性,并觸發(fā)視圖更新操作示例
這篇文章主要介紹了VUE 實(shí)現(xiàn)動(dòng)態(tài)給對(duì)象增加屬性,并觸發(fā)視圖更新操作,涉及vue.js對(duì)象屬性動(dòng)態(tài)操作及視圖更新相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11vue3?typescript封裝axios過(guò)程示例
這篇文章主要為大家介紹了vue3?typescript封裝axios過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10vue-element-admin登錄攔截設(shè)置白名單方式
這篇文章主要介紹了vue-element-admin登錄攔截設(shè)置白名單方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue報(bào)錯(cuò)Syntax?Error:TypeError:?this.getOptions?is?not?a?
前幾天在vue運(yùn)行項(xiàng)目過(guò)程中報(bào)錯(cuò)了,所以下面這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò)Syntax?Error:TypeError:?this.getOptions?is?not?a?function的解決方法,需要的朋友可以參考下2022-07-07react+?ts?vite搭建及二次封裝請(qǐng)求的過(guò)程解析
這篇文章主要介紹了react+?ts?vite搭建及二次封裝請(qǐng)求,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Vue3中watchEffect高級(jí)偵聽(tīng)器的具體使用
Vue3中新增了一種特殊的監(jiān)聽(tīng)器watchEffect,本文主要介紹了Vue3中watchEffect高級(jí)偵聽(tīng)器的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01vue-resource請(qǐng)求實(shí)現(xiàn)http登錄攔截或者路由攔截的方法
這篇文章主要介紹了vue-resource請(qǐng)求實(shí)現(xiàn)http登錄攔截或者路由攔截的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07