更新记录

1.0(2022-08-14)

请见插件说明


平台兼容性

Vue2 Vue3
×
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
× × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

下载svg-inline-loader

npm install svg-inline-loader -D

vue.config.js文件中配置svg的loader

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'
                ]
            })
    }
}

在static文件夹下创建svgs文件夹 ,将需要引用的svg文件放入该文件夹

全局组件svg-icon.vue

<template>
        <text v-html="svg"></text>
</template>

<script>
    /*
    @property {String} name  引用的svg文件名  路径:@/static/svgs/ 
    @property {String} color  颜色 
    @property {Number|String}, width  宽度 默认 30  单位:rpx
    @property {Number|String}, height  高度 默认 30  单位:rpx
    */
    export default {
        props:{
            name:{
                type:String,
            },
            color:{
                type:String
            },

            width:{   
                type:Number|String,
                default:()=>30
            },
            height:{
                type:Number|String,
                default:()=>30
            }

        },
        data() {
            return {
                svg:'',
            }
        },

        mounted() {
            const svg=require('@/static/svgs/'+this.name+'.svg')
            this.svg=svg.replace(/<svg(.+)viewBox/,`<svg fill=${this.color||null} width=${uni.upx2px(this.width)} height=${uni.upx2px(this.height)}  viewBox`)
        },

    }
</script>

<style scoped>
</style>

main.js中全局注册组件

import Vue from 'vue'
import svgIcon from './components/svg-icon.vue'
Vue.component("svgIcon", svgIcon);

在vue中使用组件

<svgIcon name="edit" width="60"  height="60"   color="blue"></svgIcon>
<svgIcon name="delete" width="60"  height="60"   color="red"></svgIcon>

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问