vbenjs / vite-plugin-svg-icons

Vite Plugin for fast creating SVG sprites.
MIT License
801 stars 109 forks source link

color值不生效 #86

Open xiumubai opened 1 year ago

xiumubai commented 1 year ago
<template>
  <i :class="['el-icon', spin && 'svg-icon-spin']" :style="getStyle">
    <svg aria-hidden="true">
      <use :xlink:href="symbolId" :fill="color" />
    </svg>
  </i>
</template>

<script setup lang="ts" name="SvgIcon">
import { computed } from 'vue'
import type { CSSProperties } from 'vue'
const props = defineProps({
  prefix: {
    type: String,
    default: 'icon',
  },
  name: {
    type: String,
    required: true,
  },
  color: {
    type: String,
    default: '#ffffff',
  },
  size: {
    type: [Number, String],
    default: 20,
  },
  spin: {
    type: Boolean,
    default: false,
  },
})

const symbolId = computed(() => `#${props.prefix}-${props.name}`)
const getStyle = computed((): CSSProperties => {
  const { size } = props
  let s = `${size}`
  s = `${s.replace('px', '')}px`
  return {
    fontSize: s,
  }
})
</script>

<style scoped lang="scss">
.el-icon {
  --color: inherit;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1em;
  height: 1em;
  font-size: inherit;
  line-height: 1em;
  color: var(--color);
  fill: currentColor;

  svg {
    width: 1em;
    height: 1em;
  }
}

.svg-icon-spin {
  animation: circle 1.5s infinite linear;
}

/* 旋转动画 */
@keyframes circle {
  0% {
    transform: rotate(0);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
caowen commented 1 year ago

When I encountered the same problem as you, how did you solve it?

edk24 commented 1 year ago

vue3

vite.config.ts / js

add code

createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      // 指定symbolId格式,这里需要与组件找那个的 symbolId 对应
      symbolId: 'icon-[dir]-[name]',

      /**
       * custom dom id
       * @default: __svg__icons__dom__
       */
      customDomId: '__svg__icons__dom__',

+      svgoOptions: {
+        full: true,
+        plugins: [
+         {
+           name: "removeAttrs",
+           params: {
+              attrs: "fill"
+            }
+          }
+        ]
+      },
    }),

batch remove fill attr

洋文不好, 加这个配置就可以了, 删除 fill attr 就可以了, 参考 #70

cMing1997 commented 2 months ago

j加上这个没用的话,可以将包替换成 @digitalacorn/vite-plugin-svg-icons 配置vite.config.ts时,在该插件的配置项中提供了选项 image replaceStrokeWithCurrentColor 问题在于 image