美文网首页
Vue3加载远程服务器的svg图标文件

Vue3加载远程服务器的svg图标文件

作者: WebGiser | 来源:发表于2024-09-10 17:23 被阅读0次

Vue3加载远程服务器的svg图标文件,并实现颜色、大小的可控

准备svg文件
<svg width="inherit" height="inherit" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect id="ic-用户" width="20.000000" height="20.000000" fill="inherit" fill-opacity="0"/>
    <path id="矢量 77" d="M6 6C6 3.79 7.8 2 10 2C12.2 2 14 3.79 14 6C14 8.2 12.2 10 10 10C7.8 10 6 8.2 6 6ZM12.5 6C12.5 4.59 11.4 3.5 10 3.5C8.59 3.5 7.5 4.59 7.5 6C7.5 7.4 8.59 8.5 10 8.5C11.4 8.5 12.5 7.4 12.5 6ZM3.5 12.7C5.59 11.6 7.8 11.1 10 11.1C12.2 11.1 14.4 11.7 16.5 12.7C16.79 12.9 17 13.2 17 13.6L17 17C17 17.6 16.6 18 16 18L4 18C3.4 18 3 17.6 3 17L3 13.6C3 13.2 3.19 12.9 3.5 12.7ZM15.5 13.9C13.8 13 11.9 12.6 10 12.6C8.09 12.6 6.19 13.1 4.5 13.9L4.5 16.5L15.5 16.5L15.5 13.9Z" fill="inherit" fill-opacity="1.000000" fill-rule="evenodd"/>
</svg>

注意点

  • width、height 值要设置为 inherit
  • fill 值设置为 currentColor
  • 内部元素标签的 fill 值设置为 inherit
封装iconSvg.vue组件

<template>
    <div
        :style="{
            color: color,
            width: width,
            height: height,
            transform: transform,
        }"
        v-html="svgContent"
    ></div>
</template>

<script setup >
import { onMounted, ref, computed, onBeforeUnmount } from "vue";
import axios from "axios";

const props = defineProps({
    type: {
        type: String,
        default: "",
    },
    color: {
        type: String,
        default: "#00ff00",
    },
    width: {
        type: String,
        default: "20px",
    },
    height: {
        type: Number,
        default: "20px",
    },
    rotate: {
        type: Number,
        default: 0,
    },
    scale: {
        type: Number,
        default: 1,
    },
});

const svgContent = ref("");
const transform = computed(() => {
    return "rotate(" + props.rotate + "deg) scale(" + props.scale + ")";
});

onMounted(() => {
    axios.get(props.type).then((res) => {
        svgContent.value = res.data;
    });
});
</script>

<style scoped lang="less">
</style>
测试 test.vue
<template>
    <div style="position: relative; top: 50%; left: 50%">
        <iconSvg
            :type="svgInfo.type"
            :color="svgInfo.color"
            :width="svgInfo.width"
            :height="svgInfo.height"
            :rotate="svgInfo.rotate"
            :scale="svgInfo.scale"
        ></iconSvg>
    </div>
</template>

<script setup >
import { onMounted, reactive, ref } from "vue";
import iconSvg from "./iconSvg/index.vue";

const svgInfo = reactive({
    type: "sidebar-user.svg",
    color: "#000000",
    width: "20px",
    height: "20px",
    rotate: 0,
    scale: 1,
});
    
setInterval(() => {
    let r = 255 * Math.random();
    let g = 255 * Math.random();
    let b = 255 * Math.random();
    svgInfo.color = `rgb(${r},${g},${b})`;
    svgInfo.width = "20px";
    svgInfo.height = "20px";
    svgInfo.rotate += 1;
    svgInfo.scale += 0.02;
}, 100);

</script>

<style scoped lang="less">

</style>

相关文章

  • svg合并

    假如页面有多个svg图标要加载,多次加载不利,可将多个svg合并为一个加载 如下有两个svg 将他们的前后缀改为s...

  • 打造自己的图标字体库

    首先给大家介绍三个网站 // 图标库,选好自己喜欢的图标下载 SVG 文件iconfont// 将 SVG 转为字...

  • 前端性能优化思路

    资源加载方面 资源缓存 CDN 图片懒加载、雪碧图 svg、字体图标 GZIP 去除注释、调试代码 解析渲染方面 ...

  • 搭建测试环境遇到的坑

    1:加载包,拷贝文件夹和连同的文件用SCP命令从远程服务器拷贝到本地服务器,scp -r 192.168.2.12...

  • SVG实现不规则图形点击

    SVG概念 SVG是一种图像文件格式,类似于PNG,JPG。只不过PNG需要图像引擎加载,SVG则是由画布来加载。...

  • SCP命令常用操作

    传输本地文件至远程服务器 传输本地目录至远程服务器 传输远程服务器文件到本地 传输远程服务器目录到本地

  • SVG 图片处理器 svgo

    最近设计同事给的 svg 文件出现了一些问题。比如我加了几个 svg 图片,实际看这几个图标的时候发现图标显示不全...

  • 手把手教你根据svg文件制作图标字体

    1,准备好本地的svg图标文件; 2,打开icomoon图标字体制作网站:https://icomoon.io/,...

  • 最简单的SVG图标库,搭建自己的图标库

    最简单的SVG图标库,只支持color 和 size 两个属性直接添加 svg文件,生成react 、vue、vu...

  • AGS JS开发-以JSON参数构建地图渲染之四

    使用SVG图标符号 1.环境说明 ArcGIS 10.4.1 JS API 3.15 2.SVG图标介绍 SVG是...

网友评论

      本文标题:Vue3加载远程服务器的svg图标文件

      本文链接:https://www.haomeiwen.com/subject/rnjuljtx.html