欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

svg图片的使用 有大用

1.将svg图片放入项目的src\assets\icons路径,如下图

       

2.封装一个组件名为SvgIcon的组件,封装如下:

<template>
  <svg aria-hidden="true" class="svg-icon">
    <use :xlink:href="symbolId" :fill="color" />
  </svg>
</template>

<script setup lang="ts">
import { computed } from 'vue';

const props = defineProps({
  prefix: {
    type: String,
    default: 'icon'
  },
  iconClass: {
    type: String,
    required: false
  },
  color: {
    type: String,
    default: ''
  }
});

const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`);
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  overflow: hidden;
  fill: currentColor;
}
</style>

3.在需要使用的地方,如下两步即可显示svg图片

//引入组件
import SvgIcon from '@/components/SvgIcon/index.vue';
//使用组件
<svg-icon
  :icon-class="islineChart?'line-chart':'build'"
  @click="islineChart = !islineChart" 
  color="#409eff"
  style="width: 30px;height: 30px;"
 />

4.显示效果如下

:icon-class为'line-chart'时


       


:icon-class为'build'时


       

来自  https://www.jianshu.com/p/fbb253b33d87  

普通分类: