700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【Vue3】Vue3+Vite前端在组件中直接使用svg图标icon(实现设计稿的icon解决方案)

【Vue3】Vue3+Vite前端在组件中直接使用svg图标icon(实现设计稿的icon解决方案)

时间:2024-03-15 11:20:09

相关推荐

【Vue3】Vue3+Vite前端在组件中直接使用svg图标icon(实现设计稿的icon解决方案)

webpack实现

下载icon图标(svg格式)

用到的是腾讯codesign,其他平台也能进行svg图片的下载

放入项目

svg中如何控制图标颜色

如下图控制svg颜色的是fill参数,此处可以删除掉,保证添加后和同级文字样式统一

引入插件

vite.config.ts中引入插件vite-plugin-svg-icons

官方文档:/vbenjs/vite-plugin-svg-icons/tree/main#readme

plugins中使用

此处需要在plugins中,匹配到svg的目录

引入svg组件

svg组件

<template><svg aria-hidden="true" :class="`iconfont ${className}`" :style="`width:${width};height:${height};color:${color};${style}`"><use :xlink:href="symbolId" /></svg></template><script lang="ts">import {computed, defineComponent } from "vue";/*** 自定义svg图标,可自行将svg图标下载后存放在/src/assets/icons/svg目录下* `使用方法:<svg-icon name="earth" color="red"></svg-icon>`*/export default defineComponent({name: "SvgIcon",props: {prefix: {type: String,default: "icon"},name: {type: String,required: true},color: {type: String,default: ""},width: String,height: String,className: {type: String, default: "" },style: {type: String, default: "" }},setup(props) {const symbolId = computed(() => `#${props.prefix}-${props.name.replace("icon-", "")}`);return {symbolId };}});</script>

svg组件使用

/*** 自定义svg图标,可自行将svg图标下载后存放在/src/assets/icons/svg目录下* `使用方法:<svg-icon name="earth" color="red"></svg-icon>`*/

<template><div class="hidden-xs-only"><svg-icon :name="state.collapseSidebar ? 'indent' : 'outdent'" /></div><div class="hidden-sm-and-up show-xs-only"><svg-icon name="icon-indent" /></div></template><script>import SvgIcon from '@/components/base/svg-icon'export default defineComponent({components: {SvgIcon},setup(){})}</script>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。