封装公用svg图标

首先创建一个 index.vue 新组件

<template>
  <svg
    class="svg-icon"
    :class="svgClass"
    :style="{ width: width + 'px', height: height + 'px' }"
    aria-hidden="true"
  >
    <use :xlink:href="iconName" />
  </svg>
</template>

<script>
export default {
  name: "SvgIcon",
  props: {
    name: {
      type: String,
      required: true,
    },
    width: {
      type: Number,
      default: 40,
    },
    height: {
      type: Number,
      default: 40,
    },
  },
  computed: {
    iconName() {
      return `#icon-${this.name}`;
    },
    svgClass() {
      if (this.name) {
        return "svg-icon" + this.name;
      } else {
        return "svg-icon";
      }
    },
  },
};
</script>

<style scoped>
.svg-icon {
  width: 100px;
  height: 100px;
  fill: currentColor;
  overflow: hidden;
}
</style>

其次要注册该组件

import Vue from 'vue'
import SvgIcon from './index.vue' // 引用新组件

// register globally
Vue.component('svg-icon', SvgIcon)

const req = require.context('@/assets/svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

最后直接在vue文件使用

<svg-icon
    name="kafkasource"
	:width="14"
	:height="12"
	/>
						
// name表示命名的svg图标文件名称
// width、height表示图标大小

在 UniApp 项目中封装 SVG 图标组件,可以通过创建一个可复用的 `SvgIcon` 组件来实现。以下是详细的封装步骤: ### 创建 SvgIcon 组件 1. 在 `components` 文件夹下创建一个 `SvgIcon` 文件夹,并在其中创建 `index.vue` 文件,内容如下: ```vue <template> <svg :width="size" :height="size" viewBox="0 0 24 24" :fill="color" xmlns="http://www.w3.org/2000/svg" > <path :d="pathData" /> </svg> </template> <script lang="ts" setup> import { defineProps } from 'vue' defineProps({ pathData: { type: String, required: true }, color: { type: String, default: '#333' }, size: { type: [String, Number], default: 24 } }) </script> ``` 2. 在 `components/SvgIcon/index.vue` 中,`pathData` 是 SVG 图标的路径数据,可以通过将 SVG 文件转换为路径数据来获取。 ### 在页面中使用 SvgIcon 组件 1. 在需要使用的页面中引入并注册 `SvgIcon` 组件: ```vue <script lang="ts" setup> import SvgIcon from '@/components/SvgIcon/index.vue' </script> ``` 2. 在模板中使用 `SvgIcon` 组件: ```vue <template> <view> <svg-icon :path-data="iconPath" color="#f00" size="32" /> </view> </template> <script lang="ts" setup> import { ref } from 'vue' const iconPath = ref('M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5') </script> ``` ### 全局注册 SvgIcon 组件 1. 在 `main.js` 或 `main.ts` 中全局注册 `SvgIcon` 组件: ```javascript import { createApp } from 'vue' import App from './App.vue' import SvgIcon from '@/components/SvgIcon/index.vue' const app = createApp(App) app.component('SvgIcon', SvgIcon) app.mount('#app') ``` 2. 然后可以在任何页面中直接使用: ```vue <template> <view> <svg-icon :path-data="iconPath" color="#0f0" size="48" /> </view> </template> ``` ### 配置 SVG 加载器(可选) 如果项目中需要内联 SVG 文件,可以配置 `svg-inline-loader` 来优化 SVG 的加载[^5]。 1. 安装 `svg-inline-loader`: ```bash npm install svg-inline-loader -D ``` 2. 在 `vue.config.js` 中配置 SVG 加载器: ```javascript const path = require('path') module.exports = { chainWebpack: config => { const svgRule = config.module.rule('svg') svgRule.uses.clear() svgRule .use('svg-inline-loader') .loader(path.resolve(__dirname, './node_modules/svg-inline-loader')) .options({ removeTags: true, removingTags: ['p-id', 'id', 'class', 'title', 'desc', 'defs', 'style'], removingTagAttrs: [ 'fill', 't', 'version', 'p-id', 'id', 'class', 'title', 'desc', 'defs', 'style', 'width', 'height', 'xmlns', 'xmlns:xlink' ] }) } } ``` 3. 将 SVG 文件放置在 `static/svgs` 文件夹中,并通过路径引用。 ### 示例代码 以下是一个完整的 `SvgIcon` 组件示例: ```vue <template> <svg :width="size" :height="size" viewBox="0 0 24 24" :fill="color" xmlns="http://www.w3.org/2000/svg" > <path :d="pathData" /> </svg> </template> <script lang="ts" setup> import { defineProps } from 'vue' defineProps({ pathData: { type: String, required: true }, color: { type: String, default: '#333' }, size: { type: [String, Number], default: 24 } }) </script> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值