ant-design-vue在vue2中自定义icon的实现

此次的目的:自定义两个icon

实现方式有好几种,此次用svg     

 这是目标效果:

这是官方社区文档中的原代码:

<template>
  <div class="custom-icons-list">
    <heart-icon :style="{ color: 'hotpink' }" />
    <panda-icon :style="{ fontSize: '32px' }" />
  </div>
</template>
<script>
const HeartSvg = {
  template: `
    <svg width="1em" height="1em" fill="currentColor" viewBox="0 0 1024 1024">
      <path d="M923 283.6c-13.4-31.1-32.6-58.9-56.9-82.8-24.3-23.8-52.5-42.4-84-55.5-32.5-13.5-66.9-20.3-102.4-20.3-49.3 0-97.4 13.5-139.2 39-10 6.1-19.5 12.8-28.5 20.1-9-7.3-18.5-14-28.5-20.1-41.8-25.5-89.9-39-139.2-39-35.5 0-69.9 6.8-102.4 20.3-31.4 13-59.7 31.7-84 55.5-24.4 23.9-43.5 51.7-56.9 82.8-13.9 32.3-21 66.6-21 101.9 0 33.3 6.8 68 20.3 103.3 11.3 29.5 27.5 60.1 48.2 91 32.8 48.9 77.9 99.9 133.9 151.6 92.8 85.7 184.7 144.9 188.6 147.3l23.7 15.2c10.5 6.7 24 6.7 34.5 0l23.7-15.2c3.9-2.5 95.7-61.6 188.6-147.3 56-51.7 101.1-102.7 133.9-151.6 20.7-30.9 37-61.5 48.2-91 13.5-35.3 20.3-70 20.3-103.3 0.1-35.3-7-69.6-20.9-101.9z" />
    </svg>
  `,
};
const PandaSvg = {
  template: `
    <svg viewBox="0 0 1024 1024" width="1em" height="1em" fill="currentColor">
      <path d="M99.096 315.634s-82.58-64.032-82.58-132.13c0-66.064 33.032-165.162 148.646-148.646 83.37 11.91 99.096 165.162 99.096 165.162l-165.162 115.614zM924.906 315.634s82.58-64.032 82.58-132.13c0-66.064-33.032-165.162-148.646-148.646-83.37 11.91-99.096 165.162-99.096 165.162l165.162 115.614z" fill="#6B676E" p-id="1143" />
      <path d="M1024 561.548c0 264.526-229.23 429.42-512.002 429.42S0 826.076 0 561.548 283.96 66.064 512.002 66.064 1024 297.022 1024 561.548z" fill="#FFEBD2" p-id="1144" />
      <path d="M330.324 842.126c0 82.096 81.34 148.646 181.678 148.646s181.678-66.55 181.678-148.646H330.324z" fill="#E9D7C3" p-id="1145" />
      <path d="M644.13 611.098C594.582 528.516 561.55 512 512.002 512c-49.548 0-82.58 16.516-132.13 99.096-42.488 70.814-78.73 211.264-49.548 247.742 66.064 82.58 165.162 33.032 181.678 33.032 16.516 0 115.614 49.548 181.678-33.032 29.18-36.476-7.064-176.93-49.55-247.74z" fill="#FFFFFF" p-id="1146" />
      <path d="M611.098 495.484c0-45.608 36.974-82.58 82.58-82.58 49.548 0 198.194 99.098 198.194 165.162s-79.934 144.904-148.646 99.096c-49.548-33.032-132.128-148.646-132.128-181.678zM412.904 495.484c0-45.608-36.974-82.58-82.58-82.58-49.548 0-198.194 99.098-198.194 165.162s79.934 144.904 148.646 99.096c49.548-33.032 132.128-148.646 132.128-181.678z" fill="#6B676E" p-id="1147" />
      <path d="M512.002 726.622c-30.06 0-115.614 5.668-115.614 33.032 0 49.638 105.484 85.24 115.614 82.58 10.128 2.66 115.614-32.944 115.614-82.58-0.002-27.366-85.556-33.032-115.614-33.032z" fill="#464655" p-id="1148" />
      <path d="M330.324 495.484m-33.032 0a33.032 33.032 0 1 0 66.064 0 33.032 33.032 0 1 0-66.064 0Z" fill="#464655" p-id="1149" />
      <path d="M693.678 495.484m-33.032 0a33.032 33.032 0 1 0 66.064 0 33.032 33.032 0 1 0-66.064 0Z" fill="#464655" p-id="1150" />
    </svg>
  `,
};

const HeartIcon = {
  template: `
    <a-icon :component="HeartSvg" />
  `,
  data() {
    return {
      HeartSvg,
    };
  },
};

const PandaIcon = {
  template: `
    <a-icon :component="PandaSvg" />
  `,
  data() {
    return {
      PandaSvg,
    };
  },
};

export default {
  components: {
    HeartIcon,
    PandaIcon,
  },
};
</script>
<style scoped>
.custom-icons-list >>> .anticon {
  margin-right: 6px;
}
</style>

在vue2项目中直接复制粘贴这一块代码肯定是运行不了的,会报警告

[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

概括起来就是 “模式不对,编译不了

具体原因:

        vue2有两种形式的代码 compiler(模板)模式和runtime模式(运行时),Vue2默认为runtime模式(具体原因是因为引入的文件),而官方社区文档中的原代码属于compiler模式的,匹配不上所以就会出现上面的错误信息

解决办法:

解决办法有好几个,我推荐就是直接别写成compiler模式的代码。直接自定义俩组件就行,很简单

demo:

文件结构:

 DiyIcon:

<template>
  <div>
    <PandaIcon :style="{ fontSize: '32px' }"></PandaIcon>
    <HeartIcon :style="{ color: 'hotpink', fontSize: '32px' }"></HeartIcon>
  </div>
</template>
<script>
import PandaIcon from './PandaIcon/index.vue';
import HeartIcon from './HeartIcon/index.vue';

export default {
  name: 'DiyIcon',
  components: {
    PandaIcon,
    HeartIcon,
  },
  data() {
    return {};
  },
};
</script>
<style lang="less" scoped></style>

PandaIcon:

<template>
  <div>
    <svg
      viewBox="0 0 1024 1024"
      width="1em"
      height="1em"
      fill="currentColor">
      <path
        d="M99.096 315.634s-82.58-64.032-82.58-132.13c0-66.064 33.032-165.162 148.646-148.646 83.37 11.91 99.096 165.162 99.096 165.162l-165.162 115.614zM924.906 315.634s82.58-64.032 82.58-132.13c0-66.064-33.032-165.162-148.646-148.646-83.37 11.91-99.096 165.162-99.096 165.162l165.162 115.614z"
        fill="#6B676E"
        p-id="1143" />
      <path
        d="M1024 561.548c0 264.526-229.23 429.42-512.002 429.42S0 826.076 0 561.548 283.96 66.064 512.002 66.064 1024 297.022 1024 561.548z"
        fill="#FFEBD2"
        p-id="1144" />
      <path
        d="M330.324 842.126c0 82.096 81.34 148.646 181.678 148.646s181.678-66.55 181.678-148.646H330.324z"
        fill="#E9D7C3"
        p-id="1145" />
      <path
        d="M644.13 611.098C594.582 528.516 561.55 512 512.002 512c-49.548 0-82.58 16.516-132.13 99.096-42.488 70.814-78.73 211.264-49.548 247.742 66.064 82.58 165.162 33.032 181.678 33.032 16.516 0 115.614 49.548 181.678-33.032 29.18-36.476-7.064-176.93-49.55-247.74z"
        fill="#FFFFFF"
        p-id="1146" />
      <path
        d="M611.098 495.484c0-45.608 36.974-82.58 82.58-82.58 49.548 0 198.194 99.098 198.194 165.162s-79.934 144.904-148.646 99.096c-49.548-33.032-132.128-148.646-132.128-181.678zM412.904 495.484c0-45.608-36.974-82.58-82.58-82.58-49.548 0-198.194 99.098-198.194 165.162s79.934 144.904 148.646 99.096c49.548-33.032 132.128-148.646 132.128-181.678z"
        fill="#6B676E"
        p-id="1147" />
      <path
        d="M512.002 726.622c-30.06 0-115.614 5.668-115.614 33.032 0 49.638 105.484 85.24 115.614 82.58 10.128 2.66 115.614-32.944 115.614-82.58-0.002-27.366-85.556-33.032-115.614-33.032z"
        fill="#464655"
        p-id="1148" />
      <path
        d="M330.324 495.484m-33.032 0a33.032 33.032 0 1 0 66.064 0 33.032 33.032 0 1 0-66.064 0Z"
        fill="#464655"
        p-id="1149" />
      <path
        d="M693.678 495.484m-33.032 0a33.032 33.032 0 1 0 66.064 0 33.032 33.032 0 1 0-66.064 0Z"
        fill="#464655"
        p-id="1150" />
    </svg>
  </div>
</template>

<script>
export default {
  name: 'PandaIcon',
  data() {
    return {};
  },
};
</script>

<style lang="less" scoped></style>

HeartIcon:

<template>
  <div>
    <svg
      width="1em"
      height="1em"
      fill="currentColor"
      viewBox="0 0 1024 1024">
      <path
        d="M923 283.6c-13.4-31.1-32.6-58.9-56.9-82.8-24.3-23.8-52.5-42.4-84-55.5-32.5-13.5-66.9-20.3-102.4-20.3-49.3 0-97.4 13.5-139.2 39-10 6.1-19.5 12.8-28.5 20.1-9-7.3-18.5-14-28.5-20.1-41.8-25.5-89.9-39-139.2-39-35.5 0-69.9 6.8-102.4 20.3-31.4 13-59.7 31.7-84 55.5-24.4 23.9-43.5 51.7-56.9 82.8-13.9 32.3-21 66.6-21 101.9 0 33.3 6.8 68 20.3 103.3 11.3 29.5 27.5 60.1 48.2 91 32.8 48.9 77.9 99.9 133.9 151.6 92.8 85.7 184.7 144.9 188.6 147.3l23.7 15.2c10.5 6.7 24 6.7 34.5 0l23.7-15.2c3.9-2.5 95.7-61.6 188.6-147.3 56-51.7 101.1-102.7 133.9-151.6 20.7-30.9 37-61.5 48.2-91 13.5-35.3 20.3-70 20.3-103.3 0.1-35.3-7-69.6-20.9-101.9z" />
    </svg>
  </div>
</template>

<script>
export default {
  name: 'HeartIcon',
  data() {
    return {};
  },
};
</script>

<style lang="less" scoped></style>

最终效果:

 控制台无报错无警告,解决问题

 👍一个👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍

以前就发现ant-design-vue的文档里好几个地方都需要用这个办法改写代码,今天整理记录了一下

<think>嗯,用户想了解在Ant-Design-Vue中使用图标的方法。首先,我需要回忆一下相关的文档和引用内容。根据提供的引用,引用[1]提到了安装Ant-Design-Vue和引入样式,这可能和图标的使用有关。引用[2]中的示例代码展示了如何在a-tree组件中使用a-icon,并指定slot来替换默认图标,这说明可以通过slot来自定义图标。引用[3]则列出了基础组件的介绍,可能包含图标组件的说明。 接下来,我需要整理图标使用的几种常见方式。Ant-Design-Vue的图标通常有两种使用方式:直接使用内置图标和自定义图标。根据版本的不同,可能还有不同的引入方式。比如,在1.x版本中,可能需要单独安装@ant-design/icons-vue,而在2.x版本中可能已经内置了图标组件。 然后,用户可能需要具体的代码示例,比如如何在组件中引入和使用图标。例如,使用a-icon标签并指定type属性来显示内置图标。另外,按需加载也是常见的需求,用户可能想知道如何仅引入需要的图标以减少包体积。此外,自定义图标的方法,比如使用slot或者替换默认图标,如引用[2]中的例子,可能需要详细说明。 还需要注意样式的引入,引用[1]中提到要在入口文件引入antd.css,否则图标可能无法显示样式。此外,主题配置可能影响图标的颜色和大小,这也是用户可能遇到的问题。 可能用户会遇到的问题包括图标不显示,这可能是由于没有正确引入样式或图标包,或者版本不兼容。需要提醒用户检查安装的版本和引入方式是否正确。另外,在自定义图标时,slot的名称是否正确,以及是否在正确的组件中使用slot,这些都需要注意。 最后,生成相关问题时,需要考虑用户可能进一步想了解的内容,比如如何自定义图标颜色、大小,或者如何处理图标的事件交互,以及性能优化方面的按需加载等。</think>### Ant-Design-Vue 图标使用教程 #### 一、图标基础使用 1. **引入图标组件** Ant-Design-Vue 1.x 需单独安装图标库,2.x 已内置图标组件。若未安装,运行以下命令: ```bash npm install @ant-design/icons-vue --save ``` 在 Vue 文件中引入: ```javascript import { UserOutlined, HomeFilled } from '@ant-design/icons-vue'; ``` 2. **直接使用内置图标** 通过`<a-icon>`组件的`type`属性指定图标类型: ```html <a-icon type="search" /> <a-icon type="arrow-up" :rotate="45" /> ``` 支持动态属性如`rotate`(旋转角度)、`spin`(旋转动画)[^3]。 #### 二、按需加载与自定义配置 1. **按需加载图标** 修改`babel.config.js`配置,仅加载使用到的图标: ```javascript module.exports = { plugins: [ [ "import", { libraryName: "@ant-design/icons-vue", libraryDirectory: "es/icons", camel2DashComponentName: false } ] ] }; ``` 2. **替换组件默认图标** 如替换 Tree 组件的展开图标(参考引用[2]示例): ```html <a-tree> <a-icon slot="switchIcon" type="caret-down"/> <a-icon slot="FolderFolder" type="dropbox"/> </a-tree> ``` 通过`slot`属性覆盖特定场景的图标[^2]。 #### 三、自定义图标 1. **使用第三方图标库** 结合`<a-icon>`封装 SVG 图标: ```html <a-icon :component="MySvgIcon" /> ``` 其中`MySvgIcon`为自定义Vue 组件。 2. **主题色适配** 通过 CSS 覆盖默认样式: ```css .anticon { color: #1890ff; /* 主题色 */ font-size: 20px; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值