svg 可改颜色,但似乎不支持微信小程序

javascript 复制代码
<!-- components/SvgIcon/SvgIcon.vue -->
<template>
  <view
    class="svg-icon"
    :class="[customClass, { 'svg-icon--clickable': !disabled && !!$attrs.onClick }]"
    :style="computedStyles"
    @click="handleClick"
    @touchstart="handleTouchStart"
    @touchend="handleTouchEnd"
  >
    <!-- 加载状态 -->
    <view v-if="loading" class="svg-icon__loading">
      <text class="loading-text">...</text>
    </view>

    <!-- 错误回退 -->
    <text v-else-if="showFallback" class="svg-icon__fallback">□</text>
  </view>
</template>

<script setup>
import { computed, ref, watch, onMounted } from 'vue'

// 定义 Props
const props = defineProps({
  // 图标名称(对应 static/svg 目录下的文件名)
  name: {
    type: String,
    required: true,
  },
  // 图标大小
  size: {
    type: [Number, String],
    default: 88,
  },
  // 图标颜色
  color: {
    type: String,
    default: 'unset',
  },
  disabledColor: {
    type: String,
    default: '#999999',
  },
  // 自定义类名
  customClass: {
    type: String,
    default: '',
  },
  // 自定义样式
  customStyle: {
    type: Object,
    default: () => ({}),
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false,
  },
  // 是否显示加载状态
  loading: {
    type: Boolean,
    default: false,
  },
})

// 定义 Emits
const emit = defineEmits(['click', 'error'])

// 响应式数据
const iconLoaded = ref(false)
const showFallback = ref(false)
const isTouching = ref(false)

// 计算属性
const iconPath = computed(() => {
  return `/static/svg/${props.name}.svg`
})

const computedSize = computed(() => {
  if (typeof props.size === 'number') {
    return props.size + 'rpx'
  }
  if (
    props.size.includes('rpx') ||
    props.size.includes('px') ||
    props.size.includes('em') ||
    props.size.includes('%')
  ) {
    return props.size
  }
  return props.size + 'rpx'
})

const computedStyles = computed(() => {
  const baseStyles = {
    width: computedSize.value,
    height: computedSize.value,
    color: props.disabled ? props.disabledColor : props.color,
    'mask-image': `url(${iconPath.value})`,
    '-webkit-mask-image': `url(${iconPath.value})`,
    pointerEvents: props.disabled ? 'none' : 'auto',
  }
  return { ...baseStyles, ...props.customStyle }
})

// 方法
const handleClick = (event) => {
  if (!props.disabled && !props.loading) {
    emit('click', event)
  }
}

const handleTouchStart = () => {
  if (!props.disabled && !props.loading) {
    isTouching.value = true
  }
}

const handleTouchEnd = () => {
  isTouching.value = false
}

// 图标加载处理
const checkIconExists = async () => {
  return new Promise((resolve) => {
    const img = new Image()
    img.onload = () => resolve(true)
    img.onerror = () => resolve(false)
    img.src = iconPath.value
  })
}

const loadIcon = async () => {
  try {
    const exists = await checkIconExists()
    if (exists) {
      iconLoaded.value = true
      showFallback.value = false
    } else {
      iconLoaded.value = false
      showFallback.value = true
      emit('error', new Error(`图标不存在: ${props.name}`))
    }
  } catch (error) {
    iconLoaded.value = false
    showFallback.value = true
    emit('error', error)
  }
}

// 监听图标名称变化
watch(() => props.name, loadIcon, { immediate: true })

// 生命周期
onMounted(() => {
  loadIcon()
})
</script>

<style scoped>
.svg-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background-color: currentColor;
  mask-repeat: no-repeat;
  mask-position: center;
  mask-size: contain;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-position: center;
  -webkit-mask-size: contain;
  transition: all 0.3s ease;
  flex-shrink: 0;
  vertical-align: middle;
}

.svg-icon--clickable {
  cursor: pointer;
}

.svg-icon--clickable:active {
  opacity: 0.7;
  transform: scale(0.95);
}

.svg-icon__loading {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.loading-text {
  font-size: 12px;
  color: inherit;
}

.svg-icon__fallback {
  font-size: 24px;
  color: #ccc;
}
</style>
相关推荐
x***r1511 天前
Notepad++ 6.6.9安装步骤详解(附Notepad++离线安装教程)
notepad++
cosinmz2 天前
图片太多太乱怎么整理?分享一个我最近常用的图片转 PDF方法
经验分享·小程序·pdf
科技互联.2 天前
2026年小程序定制市场:个性化需求激增,技术深度成竞争关键
人工智能·小程序
小羊Yveesss2 天前
2026年小程序商城的现状和发展趋势
小程序
Greg_Zhong2 天前
微信小程序如何关闭:当前渲染模式为webview?
微信小程序·微信小程序渲染引擎·渲染引擎需搭配更高基础库
橘子海全栈攻城狮2 天前
【最新源码】养老院系统管理A013
java·spring boot·后端·web安全·微信小程序
智慧景区与市集主理人2 天前
五一市集分账混乱?巨有科技智慧市集小程序实现统一收款、自动分账
大数据·科技·小程序
程序鉴定师2 天前
深圳小程序制作哪家好?2026深度市场分析与选择指南?
大数据·小程序
河北清兮网络科技2 天前
广告联盟全解析:从开发接入到运营优化,多视角拆解流量变现逻辑
小程序·app
计算机学姐2 天前
基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·信息可视化·微信小程序·uni-app