问答

Ant Design Vue 自定义 SVG 图标 Icon 的属性如何设置

作者:admin 2021-07-16 我要评论

问题描述 使用自定义SVG图标时希望设置图标的宽高等属性,在 Ant Design Vue 的文档 中找到如下描述: Icon 中的 component 组件的接受的属性如下 注意这里是 co...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

问题描述

使用自定义SVG图标时希望设置图标的宽高等属性,在 Ant Design Vue 的文档中找到如下描述:

Icon中的component组件的接受的属性如下

注意这里是 component 的属性,而不是 Icon 的属性,所以加到 Icon 标签上并不起作用,后面会有测试结果。

我对 Vue.js 还处于刚入门的水平,不知道这里是不是有一种语法可以给这个 component 设置参数,所以只能按自己的想法胡乱做了一些尝试,果然都不起作用。还望各位大佬不吝赐教。

相关代码

<template>
  <a-icon :component="logo" class="logo" />
</template>

<script>
import Vue from "vue";
import { Icon } from "ant-design-vue";
import LogoSvg from "@/assets/logo.svg";
Vue.use(Icon);

Vue.component("logo-svg", LogoSvg);

export default {
  name: "MainFrame",
  data() {
    return {
      logo: "logo-svg"
    };
  }
};
</script>

输出结果:

<i data-v-65cc6c6a="" class="logo anticon">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class="">...</svg>
</i>

你期待的结果

<i data-v-65cc6c6a="" class="logo anticon">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="56" height="56" fill="currentColor" aria-hidden="true" focusable="false">...</svg>
</i>

自己尝试过哪些方法

方法一 将属性加到 Icon 标签上
<a-icon :component="logo" class="logo" :width="56" :height="56" />

输出结果:

<i data-v-65cc6c6a="" class="logo anticon" width="56" height="56">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class="">...</svg>
</i>

属性添加到了Icon标签上,并未出现在svg标签上。

方法二 直接使用 SVG ,不使用 Icon
<logo-svg class="logo" :width="56" :height="56" />

输出结果:

<svg data-v-65cc6c6a="" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="56" height="56" class="logo">...</svg>

原有属性被覆盖,表明属性本身是有效的。

方法三 重新定义一个组件对 svg 做一次封装
Vue.component("my-logo", {
  render: h =>
    h("logo-svg", {
      attrs: {
        width: 56,
        height: 56,
        foo: "bar"
      }
    })
});

export default {
  name: "MainFrame",
  data() {
    return {
      logo: "my-logo"
    };
  }
};

输出结果:

<i data-v-65cc6c6a="" class="logo anticon">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="1em" height="1em" foo="bar" class="" fill="currentColor" aria-hidden="true" focusable="false">...</svg>
</i>

新增的属性 foo: "bar" 出现在了 svg 标签中,但 widthheight 未能覆盖原有值。

###

应该是需要透传。也就是外侧组件先要接收width和height在传给内部组件:

<template>
  // AIcon接收宽高
  <a-icon :component="my-logo" :width="56" :height="56" class="logo" />
</template>
<script>
// AIcon在渲染时会自动将width,height, fill, class属性注入在子组件上,你要做的就是在子组件中接收这些参数,并正确赋值
Vue.component("my-logo", {
  template: `<svg :with="{{width}}" height="{{height}}">...</svg>`,
  data() {
    // 接收width,height
    return {
        width:this.$options.width,
        height: this.$options.height,
    }
  }
});

export default {
  name: "MainFrame",
  data() {
    return {
      logo: "my-logo",
    };
  }
};
</script>
###

Icon 的源码翻出来看了一下,逻辑不是太复杂。看起来并没有可以设置 component 属性的设计,反而是用一套默认设置对 component 的属性进行了覆盖,这也是为什么方法三得到那样结果的原因。

核心的代码如下:

// 默认属性
export var svgBaseProps = {
  width: '1em',
  height: '1em',
  fill: 'currentColor',
  'aria-hidden': 'true',
  focusable: 'false'
};

// 合并出完整参数
  var innerSvgProps = {
    attrs: _extends({}, svgBaseProps, {
      viewBox: viewBox
    }),
    'class': svgClassString,
    style: svgStyle
  };

// 渲染时传入参数,对svg原有参数进行覆盖
    if (Component) {
      return h(
        Component,
        innerSvgProps,
        [children]
      );
    }

所以在当前版本下应该是无法修改内部 component 的参数。

###

我也遇到了同样到问题,但是看了vue-svg-loader的仓库的Example usage问题解决了
仓库地址:https://www.npmjs.com/package...

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • Ant Design Vue 自定义 SVG 图标 Icon

    Ant Design Vue 自定义 SVG 图标 Icon

  • 有申请过专利的朋友吗?

    有申请过专利的朋友吗?

  • 带emoji的字符串如何反转

    带emoji的字符串如何反转

  • v-charts地图怎么用经纬度在地图上标点

    v-charts地图怎么用经纬度在地图上标点

腾讯云代理商
海外云服务器